Search code examples
swiftuitableviewuitoolbar

Adding a UIToolBar to a TableViewController (Swift)


I'm trying to add a UIToolBar to my TableViewController but it's displaying at the end of the first prototype cell and not at the bottom of the screen. How would I go about solving this?

Here's my code:

 override func viewDidLoad() {
    super.viewDidLoad()

    theTableView.reloadData()

    let toolbar = UIToolbar()
    toolbar.frame = CGRectMake(0, self.view.frame.height - 46, self.view.frame.width, 46)
    toolbar.backgroundColor = UIColor.redColor()
    toolbar.sizeToFit()
    view.addSubview(toolbar)

} 

Solution

  • In TableViewController the table takes up all the space. If you want to include other UI elements, use ViewController and add UITableView in it. It is a little bit of extra work, but you gain more flexibility.