Swift: Add segmented controller to a navigation bar, and keep the title + nav buttons

1.9k views Asked by At

I'd like to add a segmented controller to a navigation bar, below the title.

At the moment, I've got a storyboard UITableViewController with a CoreData-sourced dynamic table, embedded in a UINavigationController. I'd like the UISegmentedController to differentiate the sorting of the table.

There seems to be obj-C solutions available stackoverflow.com/questions/29480433/ and stackoverflow.com/questions/18813563, but I'm looking for a Swift solution, that resembles the native apps. I've also tried putting the segmented controller into a table cell with little success (can't get the action recognised). I'd prefer not to abandon the table view controller.

Any suggestions?

1

There are 1 answers

0
Shane On

I replaced the UITableViewController with a UIViewController and UITableView, then added the UISegmentedController to a UIToolBar positioned below the UINavigationBar.

I used removeShadow for the navbar, then clipsToBounds for the toolbar. I also added another View to simulate the bottom shadow on the toolbar (because I couldn't get the toolbar shadow/background position function to work).