【问题标题】:UINavigationBar's title and right button misplaced when device orientation change设备方向更改时 UINavigationBar 的标题和右键错位
【发布时间】:2015-09-20 22:25:01
【问题描述】:

我正面临一个非常棘手的问题,但不知道是什么导致了这个问题。让我详细解释一下这个问题,

一般细节
我在顶部有一个 UINavigationBar,它适用于单一方向。但是当我旋转设备时,标题和右键不合适。以下是我的案例,

案例 1:
当我在纵向模式下运行应用程序时,NavigationBar 显示正常

但是当我将方向更改为横向时,会发生这种情况

案例 2:
当我在横向模式下运行时,NavigationBar 显示正常

但如果我将方向更改为纵向,则会出现这种情况

注意:我使用的是Autolayout,下面是标题和UIBarButtonItems的代码,

self.title = @"Title";

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                         style:UIBarButtonItemStylePlain target:self action:@selector(navigateToPreviouseScreen)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add"
                                                                         style:UIBarButtonItemStylePlain target:self action:@selector(addButtonTapped)];

【问题讨论】:

  • 对这些部分应用什么样的约束?
  • 您是手动添加 UINavigationBar 吗?用 UINavigationController 永远不会看到这样的东西
  • @merge 我没有对它们使用任何约束,因为它们在 UINavigationBar 中是 UIBarButtonItems
  • @vk.edward.li 不,我不会手动添加它。我刚刚从rootViewControllerUINavigationController 进行了Push segue
  • 你能告诉我更多关于 ViewControlelr 的代码吗?

标签: ios iphone autolayout uinavigationbar uistoryboard


【解决方案1】:

我只是使用您的代码在 viewDidLoad 中配置导航栏,并没有添加任何约束。它在我这边工作得很好。您是否手动添加了约束?

【讨论】:

  • 不,我没有为这些按钮或标题添加任何约束。
【解决方案2】:

尝试 translatesAutoresizingMaskIntoConstraints = NO,它是否解决了你的问题

如果不是
也试试这个 your_right_bar_button.customview.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleHeight

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2011-08-09
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多