How to hide the status bar programmatically in iOS 7?
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
![Magesh](https://i.stack.imgur.com/RranF.jpg?s=256&g=1)
Comments
-
Magesh almost 4 years
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. -
Magesh over 10 yearsIt did not work for me. I have used this same but the status bar always shown. Please help..
-
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 over 10 yearsThis doesn't work on iOS 7. The correct answer is by @user2826529
-
echo over 10 yearsAfter hiding the statusbar, how can it be shown again? Because I want to alter the statusbar visible status programmatically.
-
Andras Hatvani over 10 yearsThere is no need to perform the selector.
-
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 over 10 years@echo See my answer if you still need to do this.
-
groomsy over 10 yearsWorks for me. Ensure you set the property he mentions in the plist appropriately.
-
Teddy about 9 yearsFar the best solution. Forget all the stuff above. Apple made it way too complicated.
-
Abdullah Umer about 5 yearsSetter for 'isStatusBarHidden' was deprecated in iOS 9.0: Use -[UIViewController prefersStatusBarHidden]
-
Wayne Henderson about 4 yearsOverride code works perfectly in iOS 13, Xcode 11.3, Swift 5. UIApplication.shared.isStatusBarHidden is deprecated.