简体   繁体   中英

How to set black status bar with a background image in iPhone X?

App has an yellow image as a background, and status bar is set hidden, it works well on other devices except iPhone X.

The status bar sticks on white text color.

在此处输入图片说明

I tried to add the following code, still failed:

override var preferredStatusBarStyle: UIStatusBarStyle{
  return .default      
}

Any other suggestion?

Thanks.

Method 1:

You have to add this value to plist: " View controller-based status bar appearance " and set it to " NO ".

After that add this in AppDelegate

   var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        if #available(iOS 11.0, *) {
            if (window?.safeAreaInsets.top)! > CGFloat(0.0) || window?.safeAreaInsets != .zero {
                print("iPhone X")
                application.isStatusBarHidden = true
                //or UIApplication.shared.isStatusBarHidden = true
            }
            else {
                print("Not iPhone X")
                application.isStatusBarHidden = true
            }
        }
        return true
    }

Method 2: " View controller-based status bar appearance " and set it to " YES ". Which is by default.

As in iOS11+ setStatusBarHidden & isStatusBarHidden are deprecated, [prefersStatusBarHidden][2] is available from iOS7+, We can make status bar visibility settings over ViewController as-

struct StatusBarInfo {
    static var isToHiddenStatus = false
  }
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        if #available(iOS 11.0, *) {
            if (window?.safeAreaInsets.top)! > CGFloat(0.0) || window?.safeAreaInsets != .zero {
                print("iPhone X")
                StatusBarInfo.isToHiddenStatus = true
            }
            else {
                StatusBarInfo.isToHiddenStatus = true
                print("Not iPhone X")
            }
        }
        return true
    }

In ViewController.Swift

override var prefersStatusBarHidden: Bool {
        return StatusBarInfo.isToHiddenStatus
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM