Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent autoresizing UINavigationBar on Landscape

In generally, on the landscape mode, the navigation bar controlled by navigation controller automatically reduce its size smaller.

But I want to prevent that autoresizing.

First, I set the navigation bar hidden and used another navigation bar not controlled by navigation controller. So, I solved the problem. But I want to find the way to disable autoresizing the navigation bar on landscape without using another bar not controlled by navigation controller.

like image 407
jinbruce627 Avatar asked Aug 11 '11 09:08

jinbruce627


2 Answers

I solved this by creating a category on UINavigationBar:

@implementation UINavigationBar (customHeight)

- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize newSize = CGSizeMake(self.frame.size.width,44);
    return newSize;
}

@end

With the category created, the height now stays fixed at 44 - I don't even need to import the category anywhere.

My app uses storyboards but I'm pretty sure it would work either way.

like image 164
chris838 Avatar answered Nov 10 '22 08:11

chris838


This will work for iOS 8

extension UINavigationBar {
    public override func sizeThatFits(size: CGSize) -> CGSize {
        var newSize = CGSizeMake(UIScreen.mainScreen().bounds.width, 44)
        return newSize
    }
}
like image 25
Rami Avatar answered Nov 10 '22 06:11

Rami