objective-c setStatusBarHidden 在 iOS 9.0 中已弃用

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

setStatusBarHidden is deprecated in iOS 9.0

objective-cios9

提问by devops_engineer

I am upgrading my code from iOS 8 to iOS 9. I have a code snippet in my program [[UIApplication applicationName] setStatusBarHidden:YES];.

我正在将我的代码从 iOS 8 升级到 iOS 9。我的程序中有一个代码片段 [[UIApplication applicationName] setStatusBarHidden:YES];

I am getting the warning "setStatusBarHidden is deprecated in iOS 9.0, Use -[UIViewController prefersStatusBarHidden". If I just replace 'setStatusBarHidden' with 'prefersStatusBarHidden', I get 'instance method not found'. Can someone please suggest me how to solve this problem?

我收到警告“setStatusBarHidden 在 iOS 9.0 中已弃用,请使用 -[UIViewController prefersStatusBarHidden”。如果我只是用 'prefersStatusBarHidden' 替换 'setStatusBarHidden',我会得到'找不到实例方法'。有人可以建议我如何解决这个问题吗?

回答by Nilesh Patel

Add below code to your view controller..

将以下代码添加到您的视图控制器..

 - (BOOL)prefersStatusBarHidden {

   return NO;
}

Note :

笔记 :

  • If you change the return value for this method, call the setNeedsStatusBarAppearanceUpdatemethod.
  • For childViewController, To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the childViewControllerForStatusBarHiddenmethod.
  • 如果更改此方法的返回值,请调用该 setNeedsStatusBarAppearanceUpdate方法。
  • 对于 childViewController,要指定子视图控制器应该控制首选状态栏隐藏/未隐藏状态,请实现该childViewControllerForStatusBarHidden方法。

回答by Hyman

prefersStatusBarHiddenis available from iOS 7+.

prefersStatusBarHidden可从 iOS 7+ 获得。

Use this in Your UIViewControllerclass

在你的UIViewController课堂上使用这个

   var isHidden = true{
        didSet{
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
    override var prefersStatusBarHidden: Bool {
        return isHidden
    }

enter image description here

在此处输入图片说明

If you change the return value for this method, call the setNeedsStatusBarAppearanceUpdate() method. To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the childViewControllerForStatusBarHidden method.

如果更改此方法的返回值,请调用 setNeedsStatusBarAppearanceUpdate() 方法。要指定子视图控制器应控制首选状态栏隐藏/未隐藏状态,请实现 childViewControllerForStatusBarHidden 方法。

回答by Adam Smaka

Swift 3.1 Xcode 8.2.1

斯威夫特 3.1 Xcode 8.2.1

  1. Change in info.plist the row View controller-based status bar appearance and set it to NO

  2. In your target settings tick "Hide Status bar"

  1. 更改 info.plist 行查看基于控制器的状态栏外观并将其设置为 NO

  2. 在您的目标设置中勾选“隐藏状态栏”

Both steps are required

两个步骤都需要

回答by Gaurav Patel

you have to add method in yourViewController.m

你必须在 yourViewController.m 中添加方法

- (BOOL)prefersStatusBarHidden {

   return NO;
}

回答by Jay Choi

Here is my swift code for setting status bar hidden and style.

这是我用于设置状态栏隐藏和样式的快速代码。

extension UIViewController {

public var privateStatusBarHidden: Bool {
    return statusBarHidden
}

public var privateStatusBarStyle: UIStatusBarStyle {
    return statusBarStyle
}

public func setStatusBarHidden(hidden: Bool, animated: Bool = false) {
    statusBarHidden = hidden
    if animated {
        UIView.animate(withDuration: 0.25, animations: { 
            self.setNeedsStatusBarAppearanceUpdate()
        })
    } else {
        self.setNeedsStatusBarAppearanceUpdate()
    }
}

public func setStatusBar(style: UIStatusBarStyle) {
    statusBarStyle = style
    self.setNeedsStatusBarAppearanceUpdate()
}

    public static func swizzleStatusBarHiddenPropertyForViewController() {
    var original = class_getInstanceMethod(UIViewController.self, #selector(getter: UIViewController.prefersStatusBarHidden))
    var changeling = class_getInstanceMethod(UIViewController.self, #selector(getter: UIViewController.privateStatusBarHidden))
    method_exchangeImplementations(original, changeling)
    original = class_getInstanceMethod(UIViewController.self, #selector(getter: UIViewController.preferredStatusBarStyle))
    changeling = class_getInstanceMethod(UIViewController.self, #selector(getter: UIViewController.privateStatusBarStyle))
    method_exchangeImplementations(original, changeling)

    original = class_getClassMethod(UIViewController.self, #selector(UIViewController.swizzleStatusBarHiddenPropertyForViewController))
    changeling = class_getClassMethod(UIViewController.self, #selector(UIViewController.emptyFunction))
    method_exchangeImplementations(original, changeling)
}

@objc private static func emptyFunction() {}
}

Usage

用法

  • in lauching function
  • 在启动功能

UIViewController.swizzleStatusBarHiddenPropertyForViewController()

UIViewController.swizzleStatusBarHiddenPropertyForViewController()

  • for hide/show statusBar, in UIViewController
  • 用于隐藏/显示状态栏,在 UIViewController 中

. self.setStatusBar(hidden: true/false)

. self.setStatusBar(隐藏:真/假)

回答by Codetard

Swift 3 with Xcode 8.3.3

Swift 3 与 Xcode 8.3.3

1) Add a row in you Info.plist. enter image description here

1) 在您的 Info.plist 中添加一行。 在此处输入图片说明

2) In your ViewController ViewDidLoad() override add:

2) 在您的 ViewController ViewDidLoad() 覆盖添加:

 UIApplication.shared.isStatusBarHidden = true