Embedding a Uitableview as a Container View Within a Uiviewcontroller

Embed view controller inside container view based on selection from tableview

First disable the segue form your container view to a DestinationViewController in your storyboard.
Now load your viewController object based on your previous tableViewController selection.

//this controller will be change on tableView selection make your own logic here.
let controller = storyboard!.instantiateViewController(withIdentifier: "Second")
addChildViewController(controller)
//Set this value false if you want to set Autolayout programmatically or set it true if you want to handle it with `frame`
controller.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(controller.view)
controller.didMove(toParentViewController: self)

Adjusting Container View's Height to Match Embedded UITableViewController

I researched, I got pointed in the right direction - specifically, that this is not possible the way I thought it was.

There is no way to have constraints between the views of a parent view controller and a child view controller - neither in IB, nor in code

As far as I can see, there are two ways to solve this:

Don't use a second UIViewController. Make the root view of your child view controller an actual subview of yourself, and just have a single UIViewController.

Do not use auto layout. Manage the frame of your child view controller's view manually, and have delegate callbacks back to your parent view controller where necessary. In those delegate callbacks, your parent view controller can react to size changes of the child view controller's view.

Get embedded UITableView from container?

If a ViewController has containerViews in it, it triggers prepareForSegue Method in a ViewController that contains containerViews after viewDidLoad. There you can get reference of viewController which is embedded in a containerView.

So for example you have a containerView which is linked with viewcontroller of class TestViewController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

var vc: AnyObject = segue.destinationViewController
if vc .isKindOfClass(TestViewController) {
NSLog("GOTCHA!")
}
}

So you can refer to the tableView in a TestViewController like forexample: vc.tableView inside the if block of prepareForSegue method.

Embedding TableView in TableViewController into another view

You can use Container View.

In the storyboard create another view controller that you would use to host those controls. Then drag in Container View from the Object Library (normally the last item in the list) and place it where you want the table view to appear (which could be the whole screen). Doing this, the Container View will create another view controller. Simply delete that and ctrl drag from the Container View to your table view controller and select embed segue.

By doing this, you would not have to change anything in your current table view controller if it doesn't depend on other view controllers to give it data in order for it to work. If it does, however, you could assign the segue identifier to the embed segue, implement prepareForSegue:sender: and do whatever you would normally do with other segues.



Related Topics



Leave a reply



Submit