I want to use a custom UINavigationBar
in one of my views, which is not a part of any UINavigationController
-hierarchy. When I drag a UINavigationBar
into Storyboard, it shows like this:
This bar is 44px, which would be enough if the status bar wouldn't share this space. Because iOS7 lets us use the entire screen, including the space for the status bar, the UINavigationBar
should be 64px tall, not 44px.
If I connect the view to a UINavigationController
's hierarchy, then it shows correct:
I read somewhere that if the UINavigationBar
has the property barPosition:
set to UIBarPositionTopAttached
, then the bar would be 64px. This is, however, a readonly
-property.
My search results have only shown something I consider a "work-around". By adding a useless UINavigationController
before this UIViewController
, I can pretend to have a hierarchy, and it will add the UINavigationBar
with 64px automatically.
Is there really no way to have a 'rogue'(without the help of a navigation controller) UINavigationBar
that covers 64px?
If that is the case, is there any valid reasons as to why?
(I'm not saying the UINavigationBar
should be 64px by default, but there should be an option for it in the inspector)
(I also see people answering with programmatic ways to solve this. Even though these answers works, I'd still have to design the storyboard with that in mind (a gap). What I want to know is if it's possible to set this in storyboard, or rather, why isn't it allowed?)
On iPhone tab bars remain 49 points tall in portrait and 32 points tall in landscape. iPhone X added extra height for the Home Bar to toolbars and tab bars and their sizes are unchanged from iOS 11: 83 points tall in portrait and 53 points tall in landscape.
Personally I feel most comfortable using a navbar height of 64px. It is enough height to accommodate a logo, and there is room enough to use text in combination with symbols.
Add a title layout to a storyboard project clipTap the Edit button to the right of the clip, then tap Edit Clip. Tap the clip, then tap the Titles button at the bottom of the screen. Tap a title layout, then tap Done.
Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.
You can set the property barPosition
to UIBarPositionTopAttached
another way!
Implement -positionForBar:
in the delegate class:
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
Your navigation bar's top must also be anchored to the Top Layout Guide.
You can change the nav bar height programmatically:
[navBar setFrame:CGRectMake(0, 0, 320, 64)];
Edit: As I wrote in the comments, you may have to put this line in viewDidLayoutSubviews
to circumvent autolayout.
You can set it to attach to the top of the view using 'User Defined Runtime Attributes' in 'Identity Inspector' in Interface Builder. Set the barPosition
property.
Here's the documentation for the barPosition
property: https://developer.apple.com/library/ios/documentation/uikit/reference/UIBarPositioning_Protocol/Reference/Reference.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With