Automatically resize UINavigationBar when turning to landscape

A typical NavController behavior is to resize the port / terrain. From 44px height to 32px I believe (from the top of the head).

I use a storyboard and turn on the navigation bar in VC, not in the controller. When you rotate between a portrait and a landscape, the navigation bar does not change automatically.

I saw this answer, but it doesn’t help: Resize UINavigationBar during rotation

The navigation bar is the controller through the appearance, which is loaded into 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 does not make any difference, and since the navigation bar will be included in several controllers, I do not want to repeat the same glue code in the answer above to all my VCs to resize it while rotating.

+5
3
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin];

. , , , .

+3

, UINavigationBar intrinsicContentSize , NSContentSizeLayoutConstraint . invalidateIntrinsicContentSize .

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

  self.navigationBar?.invalidateIntrinsicContentSize();
}
+5

, .

UIBarPosition UINavBar . IB ().

// In my view controller

#pragma mark - UIBarPositioningDelegate
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
    return UIBarPositionTopAttached;
}

UINavBar IB:

  • : (0)
  • : Superview (0)
  • : Superview (0)
  • .... ! -

The height will be determined by the size of the contents at runtime. Initially, I could not get this work properly, because I had a Vertical Content Hugging Priority of 750, but that was wrong. When I changed it to the default value (250), it worked fine.

0
source

All Articles