How to prevent autoresizing UINavigationBar on Landscape
Asked Answered
B

2

8

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.

Booking answered 11/8, 2011 at 9:9 Comment(2)
why do you want to do this? this is standard iOS behavior.Cubital
@Cubital Take a look at iOS 7 Safari in landscape ;)Minutiae
D
5

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.

Director answered 7/5, 2013 at 18:15 Comment(3)
While this solves the issue, it's not good practice to try and use categories for method overriding purposes. You should do this in a subclass. Read more here: developer.apple.com/library/ios/documentation/Cocoa/Conceptual/…Blus
this solution works for the navigation bar, but it moves my toolbar up 10px or so from the bottom of the view...Graphy
This causes the elements in the bar to no longer be vertically centered. Any way to resolve that?Fizzy
A
2

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
    }
}
Accumbent answered 4/3, 2015 at 17:41 Comment(1)
Please educate the asker and all of us by shortly explaining how and why it works. That way, you can hope to get an upvote not only by jinbruce627, but also from other users.Caryloncaryn

© 2022 - 2024 — McMap. All rights reserved.