我正在将我的代码从iOS 8升级到iOS 9.我的程序中有一个代码片段
[[UIApplication applicationName] setStatusBarHidden:YES];
。
我收到警告“在iOS 9.0中不推荐使用setStatusBarHidden,使用 - [UIViewController prefersStatusBarHidden”。如果我只是用'prefersStatusBarHidden'替换'setStatusBarHidden',我会得到'找不到实例方法'。 有人可以建议我如何解决这个问题?
答案 0 :(得分:45)
将以下代码添加到视图控制器中..
setNeedsStatusBarAppearanceUpdate
注意:
childViewControllerForStatusBarHidden
方法。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)
在UIViewController
班级
var isHidden = true{
didSet{
self.setNeedsStatusBarAppearanceUpdate()
}
}
override var prefersStatusBarHidden: Bool {
return isHidden
}
如果更改此方法的返回值,请调用 setNeedsStatusBarAppearanceUpdate()方法。指定一个孩子 视图控制器应控制隐藏/取消隐藏的首选状态栏 state,实现childViewControllerForStatusBarHidden方法。
答案 3 :(得分:2)
Swift 3.1 Xcode 8.2.1
在info.plist中更改行查看基于控制器的状态栏外观并将其设置为NO
在目标设置中,勾选“隐藏状态栏”
这两个步骤都是必需的
答案 4 :(得分:2)
使用Xcode 8.3.3的Swift 3
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() {}
}
UIViewController.swizzleStatusBarHiddenPropertyForViewController()
。 self.setStatusBar(hidden:true / false)