如何仅在较小的设备上隐藏状态栏?

时间:2019-03-14 15:16:30

标签: ios swift

我已经四处走动了。我只想在小于iPhone 6的设备上隐藏状态栏。This的回答很好,但是现在不赞成使用该代码,并且会引发错误。

我遵循了this帖子中的所有建议(这非常有帮助),并且我有有效的代码,但是我必须将其复制并粘贴到每个视图控制器上。这似乎是一个坏主意。它当然不遵守DRY方法。

这是我的适用于单个视图控制器的代码:

class Step1SplashVC: UIViewController {

var hideStatusBar: Bool = false

override var prefersStatusBarHidden: Bool { return true }

override func viewDidLoad() {
    super.viewDidLoad()

    let screenSize = Int(UIScreen.main.bounds.width)

    print("screen size:", screenSize)

    if screenSize <= 320 {
        print("device is too small. need to hide status bar")
        hideStatusBar = true
        setNeedsStatusBarAppearanceUpdate()
    }

我的问题是,我该如何重构它,以便不将其复制并粘贴到项目中的每个视图控制器(总共有35个视图控制器)?

我尝试为UIViewController创建扩展,但是它一直抛出错误。

这是错误的代码:

extension UIViewController {

    private struct Holder {
        static var hideStatusBar: Bool = false
    }

    var screensize: Bool {
        get {
            return Int(UIScreen.main.bounds.width) <= 320
        }

        set {
            Holder.hideStatusBar = true
        }
    }

    override var prefersStatusBarHidden: Bool { return true }
}

我得到的错误是:

  

属性不会覆盖其超类中的任何属性

所以我在创建扩展程序上的努力没有奏效。

如何隐藏较小设备的状态栏,而不必将代码复制并粘贴到每个视图控制器上?

谢谢!

4 个答案:

答案 0 :(得分:1)

采用您编写的代码(适用于单个视图控制器),并将其放入BaseViewController的子类UIViewController中:

class BaseViewController: UIViewController {

    var hideStatusBar: Bool = false

    override var prefersStatusBarHidden: Bool { return true }

    override func viewDidLoad() {
        super.viewDidLoad()

        let screenSize = Int(UIScreen.main.bounds.width)

        print("screen size:", screenSize)

        if screenSize <= 320 {
            print("device is too small. need to hide status bar")
            hideStatusBar = true
            setNeedsStatusBarAppearanceUpdate()
        }
    }
}

然后,您可以在项目中的每个视图控制器上子类BaseViewController(替换class XYZViewController: UIViewController),并实现super.viewDidLoad(),例如:

class Step1SplashVC: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Whatever you want, specific to each VC
    }
}

答案 1 :(得分:0)

扩展不能/不应该覆盖它们扩展的类中的属性/方法; see more on that here

或者,您可以将UIViewController子类化并在子类中实现prefersStatusBarHidden,但是随后您必须将该子类应用于所有视图控制器。

还请注意,例如,如果您的应用程序流程以UINavigationController为中心,则可以在单个子类中而不是在每个UIViewController中实现更改。

使用UIViewController子类化的示例(您可能希望以不同的方式构造实现):

class MyViewControllerBaseClass: UIViewController {
    override var prefersStatusBarHidden: Bool {
        return shouldHideStatusBar 
    }

    var shouldHideStatusBar = true
}

// ……… meanwhile in some other file…

class SomeViewController: MyViewControllerBaseClass {
    // this class now inherits the above property and the implementation of prefersStatusBarHidden
}

您也可以将用于检查屏幕大小的代码放在基类中。

答案 2 :(得分:0)

您只需在AppDelegate.swift中隐藏状态栏。只需三个步骤:

1)在Info.plist中添加新行。

键:View controller-based status bar appearance

值:No

2)现在在AppDelegate.swift中添加两个变量以获取width和height,如下所示:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    //----- These lines should be added
    public var screenWidth: CGFloat {
        return UIScreen.main.bounds.width
    }
    public var screenHeight: CGFloat {
        return UIScreen.main.bounds.height
    }
    //------ End
}

3)现在,您具有设备屏幕的大小,并且有权隐藏状态栏。在AppDelegate.swift中,您可以使用以下功能(默认功能位于顶部,而第一个功能在AppDelegate.swift中):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

在此功能中,您只需检查一次屏幕尺寸,如果屏幕尺寸小于320(小于iPhone SE),则可以隐藏状态栏,如下所示:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //----- These lines should be added
        if(self.screenWidth < 320) {
            UIApplication.shared.isStatusBarHidden = true
        }
        //------ End

        return true
    }

就是这样。

通过这种方式,您只需检查一次屏幕大小并隐藏/显示状态栏。

希望有用。也对我的英语感到抱歉。

答案 3 :(得分:0)

var screenHeight:CGFloat = UIScreen.main.bounds.size.height

if screenHeight < 667 {
    UIApplication.shared.isStatusBarHidden = true
}