6

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.

4

3 回答 3

6

如果您使用自动布局,则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();
}
于 2017-02-14T11:55:13.930 回答
4
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin];

但是你没有说灵活的高度。然而,据我所知,高度正是您希望灵活的特征。

于 2013-04-30T16:50:44.110 回答
0

接受的答案对我有用,但我想转向完全基于约束的布局。

根据需要设置代理UIBarPosition上的UINavBar位置。记得在 IB 中附加代理(当然)。

// In my view controller

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

对于UINavBarIB 的约束:

  • 顶部空间至:顶部布局指南 (0)
  • 领先空间:Superview (0)
  • 培训空间:Superview (0)
  • 最重要的是......没有任何高度限制!-

高度将在运行时由内容大小决定。最初我无法正常完成这项工作,因为我的垂直内容拥抱优先级为 750,但这是错误的。当我将其更改回默认值 (250) 时,它运行良好。

于 2014-06-24T14:47:36.457 回答