在iOS 9.0中不推荐使用setStatusBarHidden

时间:2015-06-29 11:20:34

标签: objective-c ios9

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

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

6 个答案:

答案 0 :(得分:45)

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

setNeedsStatusBarAppearanceUpdate

注意:

  • 如果更改此方法的返回值,请调用 childViewControllerForStatusBarHidden方法。
  • 对于childViewController,指定子视图控制器 应控制首选状态栏隐藏/隐藏状态,执行 module IntRing : Ring = struct type t = int let zero = 0 let one = 1 let add a b = a+b let mult a b = a*b let compare = compare let equal a b = (a=b) let to_string = Int.to_string let print a = print_string (to_string a) end;; module IntRingModP (P : Int) : Ring = struct let p = P type t = int let zero = 0 mod p let one = 1 mod p let add a b = (a+b) mod p let mult a b = (a*b) mod p let compare a b = compare (a mod p) (b mod p) let equal a b = ((a mod p) = (b mod p)) let to_string a = Int.to_string (a mod p) let print a = print_string (to_string a) end;; 方法。

答案 1 :(得分:4)

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

- (BOOL)prefersStatusBarHidden {

   return NO;
}

答案 2 :(得分:4)

iOS 7 +中提供了

prefersStatusBarHidden

UIViewController班级

中使用此功能
   var isHidden = true{
        didSet{
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
    override var prefersStatusBarHidden: Bool {
        return isHidden
    }

enter image description here

  

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

答案 3 :(得分:2)

Swift 3.1 Xcode 8.2.1

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

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

  3. 这两个步骤都是必需的

答案 4 :(得分:2)

使用Xcode 8.3.3的Swift 3

1)在Info.plist中添加一行。 enter image description here

2)在ViewController中,ViewDidLoad()覆盖add:

 UIApplication.shared.isStatusBarHidden = true

答案 5 :(得分:1)

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

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() {}
}

用法

  • in lauching function
  

UIViewController.swizzleStatusBarHiddenPropertyForViewController()

  • for hide / show statusBar,in UIViewController

。 self.setStatusBar(hidden:true / false)