iOS自定义状态栏背景颜色不显示

标签 ios swift uinavigationbar

我正在尝试使用以下方法将状态栏背景颜色填充为橙色

UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

然而,我得到一个白色的状态栏,应该用橙色填充而不是按照这个例子:Customize navigation bar appearance with swift

我在 AppDelegate.swift 文件中的 didFinishLaunchingWithOptions 方法下进行设置,以将其应用于整个应用程序。

我已将我的 info.plist 编辑为以下内容:View controller-based status bar appearance => NO

有人知道我做错了什么吗?

编辑:我不确定它是否重要,但 View 位于 UITabBarController

编辑 2:这实际上发生在所有 View 中,而不仅仅是 UITabBarController

编辑 3:谢谢 @Utsav Parikh

我现在在状态栏顶部添加一个 View ,当应用程序加载状态栏时,它会短暂显示橙色,但是一旦加载完成,它就会被推离 View 并替换为通用的白色状态栏. 为什么会这样?

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
view.backgroundColor=UIColor.orangeColor()
self.window!.rootViewController!.view.addSubview(view) 

Swift 3 编辑:

使用UITabBarController

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)

没有嵌入式 Controller

我意识到有些人来这里不仅是为了状态栏,实际上也是为了导航栏,所以我在没有任何嵌入式 Controller 的情况下学习了一些技巧:

在您的 AppDelegate.swift 中添加此方法并在 didFinishLaunchingWithOptions 中调用它

func customizeAppearance() {
    UINavigationBar.appearance().barTintColor = UIColor.black
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    UITabBar.appearance().barTintColor = UIColor.black
    let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

    UITabBar.appearance().tintColor = tintColor
}

最佳答案

Swift 3 编辑:

使用 UITabBarController

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)

没有嵌入式 Controller

我意识到有些人来这里不仅是为了状态栏,实际上也是为了导航栏,所以我在没有任何嵌入式 Controller 的情况下学习了一些技巧:

在您的 AppDelegate.swift 中添加此方法并在 didFinishLaunchingWithOptions 中调用它

func customizeAppearance() {
    UINavigationBar.appearance().barTintColor = UIColor.black
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    UITabBar.appearance().barTintColor = UIColor.black
    let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

    UITabBar.appearance().tintColor = tintColor
}

感谢 @Utsav 我将以下 subview 添加到我的 UITabBarController 并且现在似乎可以正常工作:

    let view = UIView(frame:
                    CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0)
                )
    view.backgroundColor = UIColor.orangeColor()

    self.view.addSubview(view)

UITabBarController 似乎在 AppDelegate 中表现不佳。如果有人有更好的方法让我知道,但到目前为止,这是我已经想到的解决方案。

关于iOS自定义状态栏背景颜色不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341224/

相关文章:

ios - 计算出 UIView 的大小

swift - 如何将委托(delegate)函数的结果解析为字符串 - swift

ios - 无法在 iOS 设备上从 Apple Store 接收通知(fcm)

objective-c - UINavigationBar topItem/items 似乎在后面双弹出

iphone - 我可以更改 UINavigationController viewControllers 堆栈吗?

ios - Segue 不是在视觉上执行的

ios - 如何在收到推送通知后刷新 watchkit 界面 Controller

ios - 使用quicklook打开文件(从url下载)

ios - Swift - 类 Func 实例成员 'x' 不能用于类型 'y'

ios - 如何在没有导航 Controller 的情况下在导航栏中添加栏按钮。