Search code examples
iosios5uinavigationcontrolleruinavigationbaruinavigationitem

Auto Resizing UINavigationBar on rotation to landscape


The typical NavController behaviour is to resize when in port/landscape. From 44px height to 32px I beleive (from the top of my head).

I am using a storyboard and included a Navigation Bar into a VC, not controller. When rotating between portrait and landscape the navigation bar does not resize automatically.

I have seen this answer but this does not help : Resizing UINavigationBar on rotation

The Navigation Bar is controller via appearance which is loaded in the App Delegate:

UIImage *portraitImage = [UIImage imageNamed:@"mainNavBar"];
UIImage *landscapeImage = [UIImage imageNamed:@"mainNavBarLandscape"];
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];

[[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]];
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], UITextAttributeTextColor,
                                                       [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5],UITextAttributeTextShadowColor,
                                                       [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
                                                       UITextAttributeTextShadowOffset,nil]];

[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin];

Unfortunately the last part makes no difference and as the navigation bar will be included in multiple controllers I do not want to repeat the same glue code in the question above's answer to all my VCs to resize on rotation.


Solution

  • [[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin];
    

    But you didn't say flexible height. Yet height, as I understand it, is the very feature you wish to be flexible.