Swift - Toolbar follows when viewing a table while scrolling

In my UITableViewController, my toolbar follows my table as I scroll through 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 application: 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.

+3


source to share


2 answers


The problem is that since you are using UITableViewController

, with self.view.addSubview(toolbar)

, you have added your toolbar as a subheading of yours UITableViewController

view

, i.e. a UITableView

. As a subset, UITableView

the toolbar will scroll with the table.



Decision. Use UIView

containing UITableView

instead UITableViewController

if you want to customize your view controller. This way you can add items to the view that are not scopes of your view in the table.

+3


source


You can also embed your TableViewController in a navigation controller and show from storyboard or programmatically the toolbar. This standard also sticks to the bottom and stays on top of the Views content, and you have some functionality to hide it automatically under certain conditions.

enter image description here



You don't have to always use a nav controller for navigation, sometimes its handy way of doing things Xcode makes it difficult to use without changing the views already made.

0


source







All Articles