简体   繁体   English

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

[英]How to extension UIViewController for hide and show StatusBar in Swift

I use isHideStatusBar(true) and override two essential props for hide and show StatusBar in viewController 我使用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
            }
        }

how to put some line of this code in to UIViewController extension ? 如何将这段代码的某些行放入UIViewController extension

Can be with a subclass 可以带有子类

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 扩展能力仅限于包含存储的属性和覆盖的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在AppDelegate中隐藏和显示UIViewController按钮? -迅捷 - How to hide and show UIViewController button in AppDelegate? - Swift 在 MainView 上隐藏 StatusBar 和 NavigationBar 但在 Swift 的 DetailView 上显示它 - Hide StatusBar And NavigationBar On MainView But Show It On DetailView In Swift 如何快速显示一次UIVIewController - How to show once an UIVIewController in swift 如何在Swift中将函数从扩展名调用到UIViewController - How to call a function from an extension to UIViewController in Swift iOS Swift:如何在UIViewController中隐藏和显示按钮 - iOS Swift: How to hide and reveal a button in UIViewController 带委托的Swift扩展(UIViewController) - Swift Extension with delegation (UIViewController) 如何在Swift中显示带有UICollectionView的弹出式UIViewController? - How to show a pop up UIViewController with a UICollectionView in it in Swift? Swift-在UIViewController内部扩展UIView - Swift - Extension UIView inside of UIViewController Swift:来自UIViewController扩展的registerForKeyboardNotifications - Swift: registerForKeyboardNotifications from UIViewController extension 如何在 Kotlin 中扩展 Activity,如在 Swift 中扩展 UIViewController - How to extend an Activity in Kotlin like extension of UIViewController in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM