Reputation: 12072
An UITableViewController
pretty much takes up the entire view. I need a way to limit its height, width and add some shadows etc. For a clear explanation, I won't show the UITableViewController
's contents.
Without the use of a storyboard, I subviewed the UITableViewController
:
// In another UIViewController
let otherController = OtherController() // A subclass of UITableViewController
let otherControllerView = otherController.view
someView.addSubView(otherControllerView)
[...] // bunch of constraints
Notes:
In AppDelegate
, if I set the rootController
as OtherController()
, everything works as it should. If I change it back to SomeView()
, I see my modified tableView. If I should click it, it disappears.
This was the only thing that came close to my issue but sadly, I could not understand the answers provided as nothing made any sense to me.
I need to understand, why it disappears when touched etc.
view.bringSubviewToFront(...)
proved futile. I'm gessing that a tableView should be rendered in its own controller and not in another view?
Upvotes: 1
Views: 713
Reputation: 2496
You must add the instance of UITableViewController
's subclass as child view controller of the other view controller. You need to ensure few points in order to make it work. The points are as listed below:
TableViewController
viewDidLaod
since they need to be done only once)TableViewController
to adjust its view frame after the parent view controller has laid its subviews.Code here:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let vc = TableViewController()
addChildViewController(vc)
view.addSubview(vc.view)
vc.didMove(toParentViewController: self)
childVC = vc
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
childVC?.view.frame = view.frame
}
Upvotes: 1
Reputation: 13283
So just to answer this question, indeed you got two options. One is the best way, as suggested by Rakesha. Just use UITableView
. Add it as a subview. Done.
And in the future, if you really want any controller to be added onto any UIView
, remember you need to add that controller as a child. For example, in your case:
The controller of the view that will hold your UITableViewController
will add such UITableViewController
as a child.
self.addChild(yourUITableViewController)
self.whatEverViewContainer.addSubview(yourUITableViewController.view)
// Take note of the view of your tableViewController above^.
// Then setup the constraints of your yourUITableViewController.view below.
I hope this helps!
Upvotes: 5