Reputation: 5124
I'm putting together an app where some of the views are in regular UIViewControllers and use their own UINavigationBar, whereas others will be part of a navigation hierarchy inside a UINavigationController and make use of its UINavigationBar instead.
The problem is that the actual UINavigationBars that I'm seeing are different in these two cases. The ones in that use a UINavigationController's navigation bar seem unnaturally short. Here are some pictures, showing what I see in Interface Builder versus what I see at runtime.
I can't figure out what I'm doing wrong here. Why are the navigation bars different heights? How can I make them the same?
Upvotes: 0
Views: 823
Reputation: 5124
Figured out what was going on here. UINavigationController resizes the navigation bar in portrait orientation automatically, and as far as I can tell, there is no built-in way to prevent this from happening. However, with the help of this post (How to prevent autoresizing UINavigationBar on Landscape), I was able to get it working the way I wanted. My code was slightly different from the code in the linked post. I used:
@implementation UINavigationBar (CustomHeight)
- (CGSize)sizeThatFits:(CGSize)size
{
// Need to know the width of the window
CGRect bounds = self.window.bounds;
// Sometimes height and width are turned around. Take the largest of height and width as the real width.
int width = (bounds.size.width > bounds.size.height) ? bounds.size.width : bounds.size.height;
// Make sure that the
CGSize newSize = CGSizeMake(width, 44);
return newSize;
}
I'm not sure if there's a better way to get the true width - the above is clunky but works for me since my app only runs in landscape. My bigger concern is that Apple says in its UINavigationBar documentation that you shouldn't directly adjust the frame. But I guess I'm not directly doing that; presumably internal drawing methods will call this method and do their thing.
Anyway, this works for me. Hope it helps someone else.
Upvotes: 0
Reputation: 27598
I truly believe you are mixing up two different features. UINaviationbar and UIToolbar. Here is apple's human guidelines. Look at the top of the document for Navigation Bar
I will try to see if I can find a document on their dimensions. Here is one. I am sure there are better ones available
http://www.idev101.com/code/User_Interface/sizes.html
Upvotes: 2