• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 导航栏的背景图片错误

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:17:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

  • 基于 View Controller 的状态栏外观设置为 YES(默认),并且导航 Controller 以模态方式呈现。
  • 基于 View Controller 的状态栏外观设置为NO,并且状态栏最初是隐藏的。在这种情况下,导航 Controller 不需要以模态方式呈现来显示错误的图像。

为了隔离基于 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap