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:
I want the toolbar to stick to the bottom of the view, how is this achieved?
Any suggestions would be appreciated.
source to share
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.
source to share
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.
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.
source to share