效果如下:
QQ20190715-101449.gif
1.首先在plist设置里添加属性:View controller-based status bar appearance 设置为YES。
image.png
2.在需要实现动态控制状态栏的地方添加如下代码:
class ViewController: UIViewController {
///记录状态栏切换布尔值
var isShow = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if self.isShow {
self.isShow = false
} else {
self.isShow = true
}
self.setNeedsStatusBarAppearanceUpdate()
/// Call this method if the view controller's status bar attributes, such as hidden/unhidden status or style, change. If you call this method within an animation block, the changes are animated along with the rest of the animation block.
如果视图控制器的状态栏属性(例如隐藏/取消隐藏状态或样式)发生更改,请调用此方法。如果在动画块中调用此方法,则会将更改与动画块的其余部分一起设置动画。
}
override var prefersStatusBarHidden: Bool {
return isShow
}
}
此方法setNeedsStatusBarAppearanceUpdate()为UIViewController自带方法用来控制状态栏外观属性更新。
网友评论