I want to add segmented control to the navigation bar but also keep the title and buttons, like in iOS 7 Appstore purchased section (example)
I have tried adding segmented control as title view and then using prompt as the title, but then the buttons are on the same level as segmented control.
I found two solutions:
1) As suggested by neural5torm, you can add the segmented control to an UIView with the same background color of the navigation bar
You can remove UINavigationBar's hairline in this way:
This is ok for not translucent navigation bar.
If you want a translucent navigation bar:
2) Subclass UINavigationBar to create a taller bar by overriding
sizeThatFits
To use your custom navigation bar:
Title and button items will be at the bottom. Adjust their vertical positions (in the init of your custom navigation bar or via appearance proxy)
Look at the UIBarButtonItem class reference, there are also
setTitlePositionAdjustment
and other methods for back buttonWhen you create your segmented control, add it to the navigation bar
The segmented control will be at the top. Adjust its vertical position by overriding
didAddSubview
in your custom navigation barYou can find navigation bar with UISegmentedControl in Apple Sample Code: https://developer.apple.com/library/ios/samplecode/NavBar/Introduction/Intro.html
Here is my interpretation of this code (create programmatically):
My solution was this:
Add Toolbar and Segmented control to your
xib
file. Customize it as you need and connect it to an outlet of your view controller:Then, put this in the
viewDidLoad
method:I have tried solving your problem using another approach since using a navigation bar only didn't seem to work out (maybe it's because the AppStore app is using a private api but I'm not knowledgeable enough to tell for sure...) Anyway I simply used a toolbar placed just underneath the navigation bar on which I added a segmented control, all inside a regular UIViewController.
This is what it looks like in Storyboard:
And this is the result in Simulator:
Just be careful to offset the table view down to account for the vertical space used up by the toolbar. Hope this helps!
Try to create UINavigationBar subclass and let it conform to UIToolbarDelegate protocol. Then in -init method create your segment control, add it on UIToolBar and set its delegate to your custom UINavigationBar class. Then write this magic:
Good luck!
You could use navigation item Prompt property for this. Just set the property in storyboard like this and the navigation bar will automatically fade up and down nicely. Only downside is the text is kinda small.