我在iOS 9中看到setStatusBarHidden(_:withAnimation:)
现已弃用,文档说使用[UIViewController prefersStatusBarHidden]
代替但是如果我仍想用幻灯片动画隐藏状态栏,iOS 9中的替代方法是什么?
答案 0 :(得分:94)
请参阅preferredStatusBarUpdateAnimation
,
的Gif
代码
class ViewController: UIViewController {
var isHidden:Bool = false{
didSet{
UIView.animate(withDuration: 0.5) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
@IBAction func clicked(sender: AnyObject) {
isHidden = !isHidden
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
return .slide
}
override var prefersStatusBarHidden: Bool{
return isHidden
}
}
答案 1 :(得分:14)
Swift 3
class ViewController: UIViewController {
var isHidden:Bool = false
@IBAction func clicked(sender: AnyObject) {
isHidden = !isHidden
UIView.animate(withDuration: 0.5) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return UIStatusBarAnimation.slide
}
override var prefersStatusBarHidden: Bool {
return isHidden
}
}
答案 2 :(得分:11)
通过将更新移至didSet
( Swift 3 语法),我已经清理了 Leo的惊人的答案。
class ViewController: UIViewController {
@IBAction func clicked(sender: AnyObject) {
statusBarHidden = !statusBarHidden
}
var statusBarHidden = false {
didSet {
UIView.animate(withDuration: 0.5) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
}
答案 3 :(得分:2)
如果您使用目标c编码,这是解决方案:)(Leo的目标C版本:P谢谢男人!!!)
声明变量
bool isHidden;
isHidden = false;//in viewDidload()
然后在要隐藏状态栏时添加此代码
isHidden = true;
[UIView animateWithDuration:0.6 animations:^{
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}];
之后添加这两个方法
-(UIStatusBarAnimation) preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationFade;
}
-(BOOL) prefersStatusBarHidden
{ return isHidden;}
希望你的问题能得到解决(微笑)
答案 4 :(得分:0)
但这里是它的主旨:
这是一个片段:
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
get {
return .slide
}
}
private var statusBarStyle : UIStatusBarStyle = .default
override var preferredStatusBarStyle: UIStatusBarStyle {
get {
return statusBarStyle
}
}
private var statusBarStatus : Bool = false
override var prefersStatusBarHidden: Bool {
get {
return statusBarStatus
}
}
然后我可以在这样的函数中调用:(这是我的一个例子,所以忽略自定义函数)。
func sliderView(sliderView: SliderView, didSlideToPlace: CGFloat, within: CGFloat) {
let val = (within - (didSlideToPlace - sliderView.upCent))/(within)
print(val)
//Where you would change the private variable for the color, for example.
if val > 0.5 {
statusBarStyle = .lightContent
} else {
statusBarStyle = .default
}
UIView.animate(withDuration: 0.5, animations: {
sliderView.top.backgroundColor = UIColor.black.withAlphaComponent(val)
self.coverLayer.alpha = val
self.scroll.backgroundColor = colors.lightBlueMainColor.withAlphaComponent(val)
}, completion: {
value in
//If you do not call setNeedsStatusBarAppearanceUpdate() in an animation block, the animation variable won't be called it seems.
UIView.animate(withDuration: 0.4, animations: {
self.animating = true
//Where you set the status for the bar (your part of the solution)
self.statusBarStatus = false
//Then you call for the refresh
self.setNeedsStatusBarAppearanceUpdate()
})
})
}