rick
rick

Reputation: 1057

Add View to TableViewController with NavigationController

For adding a view to a UITableViewController I added the view to navigationController as below:

self.navigationController?.view.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
view.rightAnchor.constraint(equalTo: (self.navigationController?.view.rightAnchor)!).isActive = true
view.bottomAnchor.constraint(equalTo: (self.navigationController?.view.bottomAnchor)!).isActive = true
view.widthAnchor.constraint(equalToConstant: 70).isActive = true
view.heightAnchor.constraint(equalToConstant: 120).isActive = true

But when you want to push a new ViewController it will keeps the added view (myView).

I tried to add myView to view and tableView like below:

self.view.addSubview(myView)
self.tableView.addSubview(myView)

but both doesn't work. I know I can use UIViewController and add a UITableView and then it is easier to add myView to UIViewController.

Should I add myView to another view?

Upvotes: 0

Views: 40

Answers (2)

Yes it's possible in UITableViewConroller:

   let bottomView = UIView()
    bottomView.backgroundColor = .red // or your color
    bottomView.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - 78, width: tableView.frame.size.width, height: 78) // 78 or your size of view
    navigationController?.view.addSubview(bottomView)
    tableView.tableFooterView = UIView()
    bottomView.backgroundColor = .red // or your color
    bottomView.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - 78, width: tableView.frame.size.width, height: 78) // 78 or your size of view
    navigationController?.view.addSubview(bottomView)
    tableView.tableFooterView = UIView()[enter image description here][1]

Upvotes: 0

shivi_shub
shivi_shub

Reputation: 1058

The View of a UITableViewController is a UITableView, so you cannot add subviews to the controller on top of the table.

You have to derive from UIViewController to get full layout control. Instead of using UITableViewController, use a UIViewController and put a UITableView within it.

Upvotes: 1

Related Questions