xcode 优选状态栏样式不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45372550/
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 10:09:22  来源:igfitidea点击:

preferredStatusBarStyle is not working

iosobjective-cswiftxcodestatusbar

提问by Sarah

I used to use setStatusBarStylein my project and it works fine, but it is deprecated so I use preferredStatusBarStyle, that didn't work. knowing that I've:

我曾经setStatusBarStyle在我的项目中使用它并且它工作正常,但它已被弃用,所以我使用preferredStatusBarStyle,这不起作用。知道我已经:

  1. Call the method setNeedsStatusBarAppearanceUpdate.
  2. Set "View controller-based status bar appearance" to NO in info.plist
  3. Override the function

    • (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }

    this function is not called

  1. 调用方法 setNeedsStatusBarAppearanceUpdate。
  2. 在 info.plist 中将“基于控制器的状态栏外观”设置为 NO
  3. 覆盖函数

    • (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }

    这个函数没有被调用

Note: I'm using navigation controller.

注意:我正在使用导航控制器。

回答by Krunal

Here is Apple Guidelines/Instructionabout status bar change.

这是有关状态栏更改的Apple 指南/说明

If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearanceto NOin your .plistfile. And in your appdelegate> didFinishLaunchingWithOptionsadd following ine (programatically you can do it from app delegate).

如果你想设置状态栏样式,应用的水平,那么设置UIViewControllerBasedStatusBarAppearanceNO您的.plist文件。并在您的appdelegate>didFinishLaunchingWithOptions添加以下ine(以编程方式,您可以从应用程序委托中完成)。

Objective C

目标C

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];

Swift

迅速

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    application.statusBarStyle = .lightContent
    return true
}


if you want to set status bar style, at view controller level then follow these steps:

如果要设置状态栏样式,请在视图控制器级别执行以下步骤:

  1. Set the UIViewControllerBasedStatusBarAppearanceto YESin the .plistfile, if you need to set status bar style at UIViewController level only.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller.

  1. 设置UIViewControllerBasedStatusBarAppearanceYES.plist文件,如果需要设置状态栏样式仅UIViewController的水平。
  2. 在 viewDidLoad 添加函数 - setNeedsStatusBarAppearanceUpdate

  3. 覆盖视图控制器中的 preferredStatusBarStyle。

Objective C

目标C

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNeedsStatusBarAppearanceUpdate];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

Swift

迅速

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Set value of .plist according to status bar style setup level.

根据状态栏样式设置级别设置 .plist 的值。

enter image description here

在此处输入图片说明



You can set background color for status bar during application launch or during viewDidLoad of your view controller.

您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}



Here is result:



这是结果:

enter image description here

在此处输入图片说明