preferredStatusBarStyle无法正常工作 [英] preferredStatusBarStyle is not working

查看:697
本文介绍了preferredStatusBarStyle无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在项目中使用setStatusBarStyle,它可以正常工作,但是已弃用,所以我使用preferredStatusBarStyle,但没有用. 知道我已经:

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

  1. 调用方法setNeedsStatusBarAppearanceUpdate.
  2. 在info.plist中将基于视图控制器的状态栏外观"设置为否"
  3. 覆盖该功能

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

  • (UIStatusBarStyle)preferredStatusBarStyle { 返回UIStatusBarStyleLightContent; }
  • (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }

此函数未调用

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

Note: I'm using navigation controller.

推荐答案

此处为如果要设置状态栏样式,请在.plist文件中将应用程序级别设置为UIViewControllerBasedStatusBarAppearanceNO.并在appdelegate> didFinishLaunchingWithOptions中添加以下ine(通过编程,您可以从应用程序委托中进行操作).

If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file. And in your appdelegate > didFinishLaunchingWithOptions add following ine (programatically you can do it from app delegate).

目标C

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

快速

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. 如果仅需要在UIViewController级别上设置状态栏样式,请在.plist文件中将UIViewControllerBasedStatusBarAppearance设置为YES.
  2. 在viewDidLoad中添加功能-setNeedsStatusBarAppearanceUpdate

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

在视图控制器中覆盖preferredStatusBarStyle.

override preferredStatusBarStyle in your view controller.

目标C

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

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

快速

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

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

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

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

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

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:

这篇关于preferredStatusBarStyle无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆