How to hide the status bar programmatically in iOS 7?

后端 未结 12 1740
眼角桃花
眼角桃花 2020-12-05 17:00

In ios7, how can I hide the statusbar programmatically? I am using XCode 4.6.1 (ios6.1) and I want to implement this in XCode itself.

相关标签:
12条回答
  • 2020-12-05 17:42

    If you need to hide/show it on a given view controller dynamically you can do something like this.

    (Although I recommend just using - (BOOL)prefersStatusBarHidden to return your preference if you don't need it to change.)

    // view controller header 
    @interface MyViewController : UIViewController  {
        BOOL shouldHideStatusBar;
    }
    @end
    
    
    @implementation
    
    - (BOOL)prefersStatusBarHidden {
        return shouldHideStatusBar; // backed by your instance variable
    }
    
    - (void)setPrefersStatusBarHidden:(BOOL)hidden {
        shouldHideStatusBar = hidden;
    
        // Don't call this on iOS 6 or it will crash since the 
        // `setNeedsStatusBarAppearanceUpdate` method doesn't exist
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    
        // [self setNeedsStatusBarAppearanceUpdate]; // (if Xcode 5, use this)
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-05 17:42

    Try this

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
    0 讨论(0)
  • 2020-12-05 17:42

    My experience is that you need both the code and the value in the info.plist file in iOS 9 / Xcode 7.3.

    Add this to your viewDidLoad method.

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    

    Add this to your info.plist file as a boolean value and set it to NO:

    View controller-based status bar appearance
    
    0 讨论(0)
  • 2020-12-05 17:42

    If using iOS 9.0+ and Swift. If you want to have status bar hidden in some view controllers but not all - make sure to have View controller-based status bar appearance value in Info.plist set to YES else same parameters will be used across all view controllers.

    And override prefersStatusBarHidden in subclass of UIViewController

    override var prefersStatusBarHidden: Bool {
        get {
            return true
        }
    }
    

    Similar can be done to change preferredStatusBarStyle

    override var preferredStatusBarStyle: UIStatusBarStyle {
        get {
            return .lightContent
        }
    }
    
    0 讨论(0)
  • 2020-12-05 17:43

    Swift 4.1

    step1. Set View controller-based status bar appearance in your info.plist to YES

    step2. Type some code in your UIViewController, status bar will hide when you present the UIViewController.

    private var statusBarIsHidden = false
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        statusBarIsHidden = true
        setNeedsStatusBarAppearanceUpdate()
    }
    override var prefersStatusBarHidden: Bool {
        return statusBarStatus
    }
    
    0 讨论(0)
  • 2020-12-05 17:47

    In case of iOS >= 7.0 use following code :

    Syntax:

    // Present in UIViewController of UIKit Frameworks
    - (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO
    

    Usage:

    - (BOOL)prefersStatusBarHidden {
        return YES;
    }
    

    In iOS < 7.0 use following code :

    Syntax:

    // Present in UIApplication of UIKit Frameworks
    - (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_AVAILABLE_IOS(3_2);
    

    Usage:

    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    
    0 讨论(0)
提交回复
热议问题