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

Sim*_*mon 15 uinavigationbar ios swift

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

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

但是,我得到一个白色状态栏应该用橙色填充而不是遵循这个例子:使用swift自定义导航栏外观

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

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

有谁知道我做错了什么?

编辑:我不确定它是否重要,但视图是在UITabBarController中

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

编辑3:谢谢@Utsav Parikh

我现在在状态栏的顶部添加一个视图,当应用程序加载状态栏时,它只是橙色但是,一旦完成加载,它就会被推到视图之外,并被替换为通用的白色状态栏.为什么会这样?

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) 
Run Code Online (Sandbox Code Playgroud)

编辑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)
Run Code Online (Sandbox Code Playgroud)

没有嵌入式控制器

我意识到有些人不仅来到状态栏,而且实际上是导航栏,所以我在学习过程中学到了一些技巧,没有任何嵌入式控制器:

在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
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*mon 26

编辑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)
Run Code Online (Sandbox Code Playgroud)

没有嵌入式控制器

我意识到有些人不仅来到状态栏,而且实际上是导航栏,所以我在学习过程中学到了一些技巧,没有任何嵌入式控制器:

在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
}
Run Code Online (Sandbox Code Playgroud)

感谢@Utsav,我将以下子视图添加到我的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)
Run Code Online (Sandbox Code Playgroud)

该的UITabBarController似乎并没有在玩好AppDelegate中.如果有人有更好的方式让我知道,但是,截至目前这是我已经解决的解决方案.


Uts*_*ikh 16

在AppDelegate的didFinishLaunchingWithOptions中添加此代码

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)
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助你.... !!!


naz*_*naz 6

这是我在没有在NavBarController中添加VC中的视图的情况下完成的

我希望状态栏的颜色与VC视图颜色相同,所以我写道:

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.grayColor()
    self.navigationController?.navigationBar.clipsToBounds = true
}
Run Code Online (Sandbox Code Playgroud)

试试吧.