In my iOS video app status bar is hidden in some view controllers. I have done this using following code.
[[UIApplication sharedApplication] setStatusBarHidden:YES];
It works for iOS 5 and iOS 6 , but not in iOS 7.
I tried with this in particular view controller,
Eg:
-(BOOL)prefersStatusBarHidden { return YES; }
It works well, but I cant show status bar again in the parent view controller.
To hide status bar in iOS7 you need 2 lines of code
in application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions write
[[UIApplication sharedApplication] setStatusBarHidden:YES];
in info.plist add this
View-Controller Based Status Bar Appearance = NO
FIXED SOLUTION FOR SWIFT 3+ (iOS 9, 10)
1- In info.plist set below property
2- Paste below code to Root controller , To
private var isStatusBarHidden = true {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
override var prefersStatusBarHidden: Bool {
return isStatusBarHidden
}
You can call isStatusBarHidden = true
and isStatusBarHidden = false
where you want to hide/show
status bar
Update for Swift 3:
Update Info.plist with the following info:
View controller-based status bar appearance: NO
Then, in a ViewController or elsewhere:
UIApplication.shared.isStatusBarHidden = true
You should add this value to plist: "View controller-based status bar appearance" and set it to "NO".
Steps for hide status bar in iOS
1. open AppDelegate.m file, add application.statusBarHidden in didFinishLaunchingWithOptions method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
application.statusBarHidden = YES;
return YES;
}
View controller-based status bar appearance
set NO
In the Plist add the following properties.
Status bar is initially hidden = YES
View controller-based status bar appearance = NO
now the status bar will hidden.
Here is the Swift version (pre iOS9):
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.None)
}
This also works (iOS7+):
override func prefersStatusBarHidden() -> Bool {
return true
}
You also need to call:
setNeedsStatusBarAppearanceUpdate()
in say viewDidLoad()
.
Note that if you use a SplitView controller, or some other container
view controller, you also need to have it return your class when its sent childViewControllerForStatusBarHidden
. One way to do this is have a public weak var for say statusController
, and return it in this overridden method.
Swift 5
Use following steps to hide iOS Status Bar:
Final Settings Screenshot:
This is working on Xcode 10.2
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated]
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)
}
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.None)
}
I did the following and it seems to work (even in iOS 8):
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
From UIKit
>UIApplication.h
:
// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.
@property(nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden;
- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_AVAILABLE_IOS(3_2);
So should set View controller-based status bar appearance
to NO
Try that;
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
In iOS10 all I needed to do is override the prefersStatusBarHidden
var in my RootViewController
(Swift):
override var prefersStatusBarHidden: Bool {
return true
}
You should add this value to plist: "View controller-based status bar appearance" and set it to "NO".
iOS 9 onwards :
As statusBarHidden
method was Deprecated from iOS9 you need to add two values in plist as below :
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
or by User Interface Please refre below image :
As statusBarHidden
is Deprecated from iOS9 :
@property(readwrite, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController prefersStatusBarHidden]") __TVOS_PROHIBITED;
Add following line in viewdidload
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationFade];
and add new method
- (BOOL)prefersStatusBarHidden {
return YES;
}
also change info.plist file View controller-based status bar appearance" = NO
its works for me
For better understanding add some photos with comments:
App before any changes
Found in your Project Navigator
folder named Supporting Files
and click on *.plist file
After you will get different setting of your app showed. You need to add 2 keys UIStatusBarHidden
and UIViewControllerBasedStatusBarAppearance
. You can do this simply clicking on + button
After pressing + you can choose one of the key - just start to type.
Correct version:
And finally application after applying this changes:
Also, you can find alternative solution here
To answer the Susitha's question: use setNeedsStatusBarAppearanceUpdate. This will make a call to prefersStatusBarHidden to refresh with your desired status bar state. Try this:
@property (nonatomic, getter=isHideStatusBar) BOOL hideStatusBar; // Give this a default value early
- (BOOL)prefersStatusBarHidden {
return self.isHideStatusBar;
}
- (void)someMethod {
// triggered by an event or user action
[self setHideStatusBar:YES];
[self setNeedsStatusBarAppearanceUpdate];
}
If you want to see the status bar again, set your property hideStatusBar (or whatever you call it) to NO. Call preferStatusBarHidden indirectly by making another call to
[self setNeedsStatusBarAppearanceUpdate]
Add the following to your Info.plist:
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
To hide your status bar in iOS7:
Open Your plist-file, then add a add a row called "View controller-based status bar appearance" and set its value to NO
.
But it gets more interesting when there's a UINavigationController involved:
Source: Stackoverflow.com