Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - toolbar follows tableview when scrolling

In my UITableViewController, my toolbar follows my tableview when I scroll it. My code looks like this:

 override func viewDidLoad() {
        let toolbar: UIToolbar = UIToolbar()
        let checkButton = [UIBarButtonItem(title: "Done", style: .Done, target: self, action: "checkedPress")]
        toolbar.frame = CGRectMake(0, self.view.frame.size.height - 46, self.view.frame.size.width, 48)
        toolbar.sizeToFit()
        toolbar.setItems(checkButton, animated: true)
        toolbar.backgroundColor = UIColor.redColor()
        self.view.addSubview(toolbar)
    }

and it looks like this when I run the app: enter image description here

I want the toolbar to stick to the bottom of the view, how is this achieved?

Any suggestions would be appreciated.

like image 750
martin Avatar asked Nov 19 '25 03:11

martin


1 Answers

The problem is that since you're using a UITableViewController, with self.view.addSubview(toolbar), you've added your toolbar as a subview of your UITableViewController's view, i.e. a UITableView. As a subview of the UITableView, the toolbar will scroll along with the table.

The solution: Use a UIView containing a UITableView instead of using a UITableViewController if you'd like to customize your view controller. That way you can add elements to the view that aren't subviews of your tableview.

like image 88
Lyndsey Scott Avatar answered Nov 21 '25 16:11

Lyndsey Scott