0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.