Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 787
StuartM Avatar asked Apr 30 '13 16:04

StuartM


2 Answers

If you are using autolayout, the UINavigationBar's intrinsicContentSize updates correctly on rotation but its height NSContentSizeLayoutConstraint doesn't. Calling invalidateIntrinsicContentSize manually tells autolayout to update the height constraint for the navigation bar.

// View controller
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
{
  super.viewWillTransition(to: size, with: coordinator);

  self.navigationBar?.invalidateIntrinsicContentSize();
}
like image 142
Sean Q Avatar answered Sep 21 '22 01:09

Sean Q


[[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.

like image 42
matt Avatar answered Sep 20 '22 01:09

matt