Hide the status bar in ios 9

2019-01-10 22:53发布

How do you hide the status bar in ios 9?

This is now deprecated:

 [UIApplication sharedApplication] setStatusBarHidden:YES];

7条回答
Explosion°爆炸
2楼-- · 2019-01-10 23:07

If for some reason you need View controller-based status bar appearance equal to YES (for example to keep status bar white)

on AppDelegate's didFinishLaunchingWithOptions method or wherever you setup your navigationController:

yourNavigationController.navigationBar.barStyle = .black

then use alex-staravoitau's awesome answer and add this code wherever you'll be hiding the status bar:

override var preferredStatusBarStyle: UIStatusBarStyle {
  return .lightContent
}

I'm not sure if this is the right way to achieve this result, but it worked for me and I hope it helps someone else too :)

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-10 23:08

Here's how do you easily return a control over status bar visibility for iOS 9+ and Swift 3+:

  1. Add View controller-based status bar appearance key with YES value to Info.plist.
  2. Add this variable to the view controller:

    private var isStatusBarHidden = false {
        didSet {
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
  3. Override prefersStatusBarHidden property:

    override var prefersStatusBarHidden: Bool {
        return isStatusBarHidden
    }
    

That's it. Now you are able to call isStatusBarHidden = true and isStatusBarHidden = false whenever you want.

查看更多
做个烂人
4楼-- · 2019-01-10 23:09

An easy approach would be to set the windowLevel of the Application to be either normal or statusBar based on your needs, so to start

Objective-C

To Hide the Status Bar

 UIApplication.sharedApplication.keyWindow.windowLevel = UIWindowLevelStatusBar;

To Show the Status Bar

 UIApplication.sharedApplication.keyWindow.windowLevel = UIWindowLevelNormal;

Also add the Key (View controller-based status bar appearance) with boolean value NO.

enter image description here

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-10 23:10

In most of the iOS it will work. I have tested with iOS 10.

  1. Open info.plist
  2. "View controller-based status bar appearance" set to NO
  3. "Status bar is initially hidden" set to YES
  4. Done
查看更多
冷血范
6楼-- · 2019-01-10 23:22

in info.plist add the following two property.

View controller-based status bar appearance (NO)

Status bar is initially hidden (YES)
查看更多
干净又极端
7楼-- · 2019-01-10 23:24

I know that the documentation of setStatusBarHidden: does not mention on what use instead. But the header of UIApplication does.

// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.
@property(readwrite, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController prefersStatusBarHidden]");
- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_DEPRECATED_IOS(3_2, 9_0, "Use -[UIViewController prefersStatusBarHidden]");

Here is stated that you should use the prefersStatusBarHidden on UIViewController and use view controller based statusbar styles.

All you need to do now is configure whether the view controller needs to show of hide the status bar. Like so :

- (BOOL)prefersStatusBarHidden {
   return YES;
}
查看更多
登录 后发表回答