OGeek|极客世界-中国程序员成长平台

标题: ios - 导航栏的背景图片错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:17
标题: ios - 导航栏的背景图片错误

我在 iOS 8 中遇到了这个问题,其中导航栏的背景图像在以下任何一种情况下都会显示错误:

为了隔离基于 View Controller 的状态栏外观设置为 YES(默认)并且导航 Controller 以模态方式呈现的问题,我按照以下步骤从头开始创建了一个测试项目:

  1. 使用“Master-Detail Application”模板创建一个新项目。

  2. 打开 Main.storyboard 并向其中添加一个导航 Controller 。移除它的 Root View Controller 并用 modal segue 连接主视图 Controller 。然后连接细节 View Controller 作为它的 Root View Controller 。你应该得到这样的结果:

enter image description here

  1. 通过外观代理自定义导航栏背景。为纵向 (UIBarMetricsDefault) 和横向 (UIBarMetricsCompact) 使用两个不同的图像。我在 UIImage 上使用了一个类别来从纯色创建图像。

    - (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
        // Override point for customization after application launch.
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor blueColor]] forBarMetrics:UIBarMetricsCompact];
    
        return YES;
    }
    
  2. 运行应用程序。主视图 Controller 为纵向和横向显示右侧导航栏背景图像:

enter image description here

enter image description here

  1. 现在以模态方式呈现细节 View Controller 。为此,请点击“添加”按钮,然后选择新创建的行。详细 View Controller 显示导航栏背景图像。

  2. 旋转界面。详细 View Controller 将不会更改导航栏背景图像(错误):

enter image description here

  1. 将界面旋转回原来的方向。详细 View Controller 将更改导航栏背景图像(错误):

enter image description here

还有其他人为此苦苦挣扎吗?



Best Answer-推荐答案


是的,我一直在为此苦苦挣扎,在目标的 plist 文件中添加 "View controller-based status bar appearance" 后,它在 iOS 8 上再次运行,记得将其设置为

在 MasterViewController 中,添加此项以避免 detailViewController 关闭时出现错误的背景图像。它基本上再次设置了相同的外观。

- (void)viewDidAppearBOOL)animated
{
    [super viewDidAppear:animated];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor blueColor]] forBarMetrics:UIBarMetricsCompact];
}

关于ios - 导航栏的背景图片错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26575546/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4