如何扩展 UIViewController 以在 Swift 中隐藏和显示 StatusBar

How to extension UIViewController for hide and show StatusBar in Swift

我使用 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
            }
        }

如何将此代码的某些行放入 UIViewController extension

可以带子类

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

}

Extension ability is limited to contain stored properties & overrided methods