How to hide the status bar programmatically in iOS 7?

31,673

Solution 1

in iOS7 you should implement in your viewController

- (BOOL)prefersStatusBarHidden {
    return YES;
}

Solution 2

you can hide status bar to set the key value "View controller-based status bar appearance" NO in plist. This is easiest way.

or You can hide in code by using property statusBarHidden of UIApplication class.

[[UIApplication sharedApplication] setStatusBarHidden:YES];

Swift 3.0

Hide status bar for any particular view controller

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

Hide Status bas across the application

UIApplication.shared.isStatusBarHidden = true

and set the key value "View controller-based status bar appearance" NO in info plist of project.

Solution 3

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    else
    {
        // iOS 6
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }
}

// Add this method
- (BOOL)prefersStatusBarHidden {
    return YES;
}

Solution 4

To hide for a specific ViewController (and then turn back on) when View controller-based status bar appearance set to NO:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}

Solution 5

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
Share:
31,673
Magesh
Author by

Magesh

Now i am working as a iPhone Programmer in SDI

Updated on August 24, 2020

Comments

  • Magesh
    Magesh almost 4 years

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

  • Magesh
    Magesh over 10 years
    It did not work for me. I have used this same but the status bar always shown. Please help..
  • user2826529
    user2826529 over 10 years
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; This will work and there is a property in info.plist where you can hide the status bar.
  • Vibhor Goyal
    Vibhor Goyal over 10 years
    This doesn't work on iOS 7. The correct answer is by @user2826529
  • echo
    echo over 10 years
    After hiding the statusbar, how can it be shown again? Because I want to alter the statusbar visible status programmatically.
  • Andras Hatvani
    Andras Hatvani over 10 years
    There is no need to perform the selector.
  • zekel
    zekel over 10 years
    @AndrasHatvani The question specified using Xcode 4, which means that he doesn't have the iOS 7 API. He uses performSelector to avoid "no method found" warning.
  • zekel
    zekel over 10 years
    @echo See my answer if you still need to do this.
  • groomsy
    groomsy over 10 years
    Works for me. Ensure you set the property he mentions in the plist appropriately.
  • Teddy
    Teddy about 9 years
    Far the best solution. Forget all the stuff above. Apple made it way too complicated.
  • Abdullah Umer
    Abdullah Umer about 5 years
    Setter for 'isStatusBarHidden' was deprecated in iOS 9.0: Use -[UIViewController prefersStatusBarHidden]
  • Wayne Henderson
    Wayne Henderson about 4 years
    Override code works perfectly in iOS 13, Xcode 11.3, Swift 5. UIApplication.shared.isStatusBarHidden is deprecated.