darkginger
darkginger

Reputation: 690

Make Toolbar fixed to the bottom of a UITableView in Swift 3

I am new to Swift, and I am struggling to find a solution: in my UITableViewController, I want to make a Toolbar that I have added to the storyboard fixed or "sticky" to the bottom of the view at all times.

Currently, after dragging a toolbar into the UITableViewController, it does add this toolbar; however, it places the toolbar as the final row of the table.

I want this toolbar to sit above the navigation tab bar at all times, making it fixed or sticky.

enter image description here

I have tried changing the View's content mode (by setting it to Bottom in the Storyboard) but it does not make a change in the UI.

Is there a way to set this programmatically to always show at the bottom and let the table scroll above it?

Upvotes: 1

Views: 1624

Answers (1)

Mahendra
Mahendra

Reputation: 8904

//create a tool bar and add it in view
let toolbar = UIToolbar()
toolbar.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(toolbar)

Set constraints to toolbar...

//top constraint to toolbar with tableview
self.toolbar.topAnchor.constraint(equalTo: tableview.bottomAnchor).isActive = true

//bottom constraint to toolbar with super view
self.view.bottomAnchor.constraint(equalTo: toolbar.bottomAnchor).isActive = true

//leading constraint to toolbar with super view
self.view.leadingAnchor.constraint(equalTo: toolbar.leadingAnchor).isActive = true

//trailing constraint with toolbar with super view
self.view.trailingAnchor.constraint(equalTo: toolbar.trailingAnchor).isActive = true

Upvotes: 2

Related Questions