xcode 如何使用swift更改一个视图控制器中的状态栏颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35062162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:28:26  来源:igfitidea点击:

how to change statusbar color in one view controller using swift?

iosxcodeswift

提问by Ivan A

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

I use this one to change status bar to light in all app. But now I need to change it in just one View Controller back to black. How can I do that?

我使用这个来将所有应用程序中的状态栏更改为亮起。但现在我只需要在一个 View Controller 中将它改回黑色。我怎样才能做到这一点?

回答by Mark Moeykens

Xcode 8.1, Swift 3 Solution with @IBDesignable

Xcode 8.1,带有@IBDesignable 的 Swift 3 解决方案

This solution is a little bit different:

这个解决方案有点不同:

  • Subclass of UIViewController to centralize logic
  • No code for viewDidLoad or viewDidDisappear
  • Uses @IBDesignable so you can set your status bar color in the Attributes Inspector on the Storyboard
  • UIViewController 的子类来集中逻辑
  • 没有用于 viewDidLoad 或 viewDidDisappear 的代码
  • 使用 @IBDesignable 以便您可以在 Storyboard 的 Attributes Inspector 中设置状态栏颜色

Step 1 - Setup Info.plist File

步骤 1 - 设置 Info.plist 文件

Info.plist

信息表

Step 2 - Subclass UIViewController

第 2 步 - 子类 UIViewController

import UIKit

@IBDesignable
class DesignableViewController: UIViewController {

    @IBInspectable var LightStatusBar: Bool = false

    override var preferredStatusBarStyle: UIStatusBarStyle {
        get {
            if LightStatusBar {
                return UIStatusBarStyle.lightContent
            } else {
                return UIStatusBarStyle.default
            }
        }
    }
}

Step 3 - Inherit from DesignableViewController

第 3 步 - 从 DesignableViewController 继承

Change the code for your ViewController(s) from:

将您的 ViewController(s) 的代码从:

class ViewController: UIViewController {

To:

到:

class ViewController: DesignableViewController {

Step 4 - Set your preference on the Storyboard

第 4 步 - 在 Storyboard 上设置您的偏好

Select the ViewControllers on the Storyboard and go to the Attributes Inspector: Attributes Inspector

选择 Storyboard 上的 ViewControllers 并转到 Attributes Inspector: 属性检查器

Step 5 - Run project and test

第 5 步 - 运行项目并测试

In my project I setup a Tab Bar Controller with 2 View Controllers and switch back and forth between the two. Seems to work OK for me. Light Status BarDark Status Bar

在我的项目中,我设置了一个带有 2 个视图控制器的标签栏控制器,并在两者之间来回切换。对我来说似乎工作正常。 指示灯状态栏深色状态栏

回答by MarkHim

Set View controller-based status bar appearancein your project.plist to NO

View controller-based status bar appearance在您的 project.plist 中设置为NO

Use viewWillAppearand will viewWillDisappearto set and reset the statusBarStyle, while keeping a property with the previous statusBarStyle like this

使用viewWillAppearand willviewWillDisappear来设置和重置 statusBarStyle,同时保持一个属性与之前的 statusBarStyle 像这样

let initialStatusBarStyle : UIStatusBarStyle

func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    initialStatusBarStyle = UIApplication.sharedApplication().statusBarStyle
    UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: animated)
}

func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.sharedApplication().setStatusBarStyle(initialStatusBarStyle, animated: animated)
}

回答by reza_khalafi

Solved:
Swift 3.1

已解决:
Swift 3.1

Just using this code in View Controller:

只需在视图控制器中使用此代码:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

回答by scurioni

Swift 3

斯威夫特 3

Set View controller-based status bar appearance in your project.plist to NO

将 project.plist 中基于视图控制器的状态栏外观设置为 NO

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.setStatusBarStyle(.default, animated: animated)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.setStatusBarStyle(.lightContent, animated: animated)
}

回答by Mutawe

An Objective-Canswer:

一个Objective-C答案:

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

回答by Amul4608

 let color = UIColor(red:0.00, green:0.60, blue:0.48,alpha:1.0)
        UINavigationBar.appearance().tintColor = UIColor.blue
        UINavigationBar.appearance().barTintColor = color

OR

或者

self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

回答by Gangireddy Rami Reddy

In swit4 this working fine in my project based on navigation bar

在基于导航栏的 swit4 中,这在我的项目中运行良好

   let app = UIApplication.shared
   let statusBarHeight: CGFloat = app.statusBarFrame.size.height

   let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
   statusbarView.backgroundColor = UIColor.red
   view.addSubview(statusbarView)

回答by Hemang

If you have derived your view controllers from a common view controller then, you can simply do it like this:

如果你从一个普通的视图控制器派生了你的视图控制器,你可以简单地这样做:

Step 1: Add this key to your app's info.plistfile.

第 1 步:将此密钥添加到您的应用程序info.plist文件中。

enter image description here

在此处输入图片说明

Step 2: overridethis in common view controller (or a ParentViewController).

第 2 步: override这在公共视图控制器(或 a ParentViewController)中。

override var preferredStatusBarStyle: UIStatusBarStyle {
    if self is YourChildViewController {
        return .lightContent
    }
    return .default
}

That's it! No more fancy things.

就是这样!没有更多花哨的东西。

回答by Paresh Mangukiya

When you're in a navigation controller that will not get called. The navigation controller's preferredStatusBarStyle will be called. Try this along with your code:

当你在一个不会被调用的导航控制器中时。导航控制器的 preferredStatusBarStyle 将被调用。试试这个和你的代码:

extension UINavigationController {

open override var preferredStatusBarStyle: UIStatusBarStyle {
    return topViewController?.preferredStatusBarStyle ?? .default
    }
}

and also write this:

还要写这个:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}