UINavigationController barstyle 属性改变布局

Posted

技术标签:

【中文标题】UINavigationController barstyle 属性改变布局【英文标题】:UINavigationController barstyle property changes layout 【发布时间】:2011-09-02 18:20:25 【问题描述】:

我有一个显示导航控制器的模态视图控制器。导航控制器反过来有一个常规的 UIViewController 作为它的根视图控制器。上述 UIViewController 唯一的 UI 元素是 UISwitch。

现在的问题是:当我更改导航控制器的 barStyle 属性时,UIViewController 内的 UISwitch 的布局发生了变化。这就是我要说的:

如果我不设置 barStyle 属性,这就是我得到的:

http://img535.imageshack.us/img535/2281/plaini.png

UISwitch 现在处于“预期”位置。

现在如果我设置 barStyle 属性,

navController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

注意 UISwitch 在导航栏的后面:

http://img853.imageshack.us/img853/2377/blackya.png

这是 UIViewController 中 UISwitch 的代码:

- (void)viewDidLoad

    UISwitch* mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    [self.view addSubview:mySwitch];
    [mySwitch release];

有人可以帮我了解发生了什么吗?

【问题讨论】:

【参考方案1】:

当您使用 UIBarStyleBlackTranslucent(实际上已弃用)属性时,框架会发生变化,因为它假定您希望视图位于下方

Apple Documentation 表示要使用以下内容,因为 UIBarStyleBlackTranslucent 已弃用:

navController.navigationBar.barStyle = UIBarStyleBlack;
navController.navigationBar.translucent = YES;

您可以尝试将视图移回正确的位置或尝试使用以下方法:

navController.navigationBar.tintColor = [UIColor blackColor];
navController.navigationBar.translucent = YES;

【讨论】:

我不认为 barStyle 会对视图的外观产生如此大的影响。非常感谢!!令人惊讶的是 Xcode 没有将 UIBarStyleBlackTranslucent 显示为已弃用的选项! 此外,尽管使用了您提供的上述两行代码,我还是不得不将我的 UI 元素向下移动。看起来“.translucent”属性决定了它后面的框架是否需要移动,因为当我将属性设置为NO时,我不必再移动了。 不确定您是如何布置代码的,但您不能将添加按钮的视图向下移动 44 像素吗?

以上是关于UINavigationController barstyle 属性改变布局的主要内容,如果未能解决你的问题,请参考以下文章

iOS:如何在现有 UINavigationController 中打开另一个 UINavigationController?

带有主 UINavigationController 和详细 UINavigationController 的 UISplitViewcontroller

带有 UINavigationController 的 UITabBarController,在 hidesBottomBarWhenPushed 上隐藏 UINavigationController

在 UINavigationController 内的 UITabBarcontroller 中添加 UINavigationController?

UINavigationController 与 AppDelegate 中的 UISegmentedControl 切换 UINavigationController 的 rootviewcontr

从一个 UINavigationController 到另一个 UINavigationController (Swift iOS 9, xcode 7)