Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to change the height of a UINavigationBar in Storyboard without using a UINavigationController?

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:

UINavigationBar

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:

UINavigationController

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?)

like image 519
Sti Avatar asked Dec 07 '13 08:12

Sti


People also ask

What is the height of navigation bar iPhone?

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.

What is navigation bar height?

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.

How do you add a title to a storyboard?

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.

How do I use the navigation bar in Xcode?

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.


3 Answers

You can set the property barPosition to UIBarPositionTopAttached another way!

  1. Add a delegate to your UINavigationBar.
  2. 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.

like image 99
user3269713 Avatar answered Oct 19 '22 18:10

user3269713


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.

like image 31
Lyndsey Scott Avatar answered Oct 19 '22 17:10

Lyndsey Scott


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.

enter image description here

Here's the documentation for the barPosition property: https://developer.apple.com/library/ios/documentation/uikit/reference/UIBarPositioning_Protocol/Reference/Reference.html

like image 10
joerick Avatar answered Oct 19 '22 19:10

joerick