如何在 Swift 中扩展 UIViewController 以隐藏和显示 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
            }
        }

如何将此代码的某些行放入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)
    } 

}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

iOS Swift:如何隐藏和显示 UIViewController 中的按钮

如何在Flutter中隐藏Android StatusBar

如何在Swift中从扩展调用函数到UIViewController

如何在Swift中的UIViewController和NSObject之间传递数据

如何在Swift中调用UIViewController的transitionFromViewController?

如何在iOS Swift 3中以编程方式推送和呈现给UIViewController而不进行选择

如何在Swift的UIViewController中添加多个集合视图?

如何在Swift 3中比较UIViewController?

如何在macOS的Swift Playgrounds中呈现UIViewController?

在Swift中,如何重用UIVIewController中的属性

如何在Swift中通过动作显示/隐藏UISearchBar?

Swift中的动画显示和隐藏视图

Swift 中主 UIViewController 中的多个 UIViewController

如何在Swift中扩展协议

如何在Swift中访问UIColor的扩展?

如何在OpenLayers 3中隐藏和显示功能?(重画?)

如何在android中隐藏和显示进度条?

如何在angularjs中单击隐藏和显示按钮?

如何在反应中隐藏和显示div

如何在angularjs中隐藏和显示HTML元素?

如何在PySide中显示和隐藏QtGui.QGridLayout

我如何在jQuery Mobile中隐藏和显示图像

如何在Android中单击按钮隐藏和显示密码?

如何在Flutter中动态隐藏和显示底栏

如何在react js中隐藏和显示div

如何在Angle 4.0中隐藏和显示

如何在BottomNavigationView中隐藏和显示菜单?

如何在React Native中隐藏和显示导航栏?

如何在React Native Video中显示和隐藏字幕