中隐藏和显示StatusBar

中隐藏和显示StatusBar

我使用isHideStatusBar(true)并覆盖viewController中隐藏和显示StatusBar的两个基本道具

        var statusBarShouldBeHidden = false
        override var prefersStatusBarHidden: Bool {
            return statusBarShouldBeHidden
        }

        override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
            return .slide
        }

        func isHideStatusBar(_ bool: Bool, _ delay : CFTimeInterval = 0){
            statusBarShouldBeHidden = bool
            UIView.animate(withDuration: 0.4, delay: delay, options: [], animations: {
                self.setNeedsStatusBarAppearanceUpdate()
            }) { (finished) in
            }
        }

如何将此代码的某一行放入UIViewControllerextension

最佳答案

可以与子类一起

class MainViewController: UIViewController {

    var statusBarShouldBeHidden = false
    override var prefersStatusBarHidden: Bool {
        return statusBarShouldBeHidden
    }

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return .slide
    }

    func isHideStatusBar(_ bool: Bool, _ delay : CFTimeInterval = 0){
        statusBarShouldBeHidden = bool
        UIView.animate(withDuration: 0.4, delay: delay, options: [], animations: {
            self.setNeedsStatusBarAppearanceUpdate()
        }) { (finished) in
        }
    }
}
class ViewController: MainViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        isHideStatusBar(true)
    }

}

扩展能力仅限于包含存储的属性和重写的方法

关于swift - 如何扩展UIViewController以在Swift中隐藏和显示StatusBar,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56221338/

10-10 22:25