【问题标题】:iOS: setting statusbar color without using UINavigationControlleriOS:不使用 UINavigationController 设置状态栏颜色
【发布时间】:2015-06-28 18:08:39
【问题描述】:

在我的 iOS 应用程序中,我使用 UINavigationBar 没有 UINavigationController。如何仍然设置应用程序状态栏的颜色以匹配我的 UINavigationBar 的颜色,这是自定义的蓝色?试过了还是不行:

  • 在 plist 中设置 UIViewControllerBasedStatusBarAppearance 属性没有任何效果
  • 设置我的 UINavigationBar 的barTintColor 不会影响状态栏,大概是因为它不是由 UINavigationViewController 管理的?
  • 设置UINavigationBar.appearance().barTintColor 也不起作用

有什么建议吗?

【问题讨论】:

    标签: ios cocoa-touch uinavigationbar


    【解决方案1】:

    好的,多亏了这个问题中接受的答案,我才弄明白: How to add custom navigation bar to a full-screen VC in iOS 7 and make it tint the status bar to match?

    在您的视图控制器中采用 UINavigationBarDelegate 协议并实现以下方法(Swift):

    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.TopAttached
    }
    

    显然,以这种方式定位的所有导航栏都将其 barTintColor 扩展到状态栏。

    【讨论】:

    • 这对我有用 - 请记住,您需要确保您的视图控制器设置为 UINavigationBar 的委托,或者在代码中明确地使用:myNavigationBar.delegate = self in viewDidLoad 或使用接口builder 并将导航栏的代理出口连接到您的视图控制器
    【解决方案2】:

    将此方法添加到您的视图控制器:

    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        return UIStatusBarStyleLightContent; // or UIStatusBarStyleDefault
    }
    

    【讨论】:

    • 感谢您的回复,但请阅读我的问题,我想匹配我的 UINavigationBar 的 color,在这种情况下是自定义的蓝色。
    • 状态栏只有两种变体。匹配到底是什么意思?
    • 好吧,如果你设置 UINavigationBar 的 barTintColor,当它由 UINavigationController 管理时,状态栏将获得相同的颜色。我的问题是:当您单独使用 UINavigationBar 时不会发生这种情况......
    猜你喜欢
    • 2020-02-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    相关资源
    最近更新 更多