How to retrieve view controller that currently controls status bar appearance? - ios

Is there a way to retrieve, from anywhere in an app, the view controller that is currently controlling the status bar appearance?

Dont think you can retrieve in a controller that way, but you can change the style of the status bar.
1) set the UIViewControllerBasedStatusBarAppearance to YES in the plist
2) in viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];
3) add the following method:
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}

Related

Setting statusBar style does not work Objective-C

I am trying to change my statusbar style (the color of status bar text, more specifically) depending on which viewController is active through this:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
But that does not work. Rather, it makes the whole navigationBar black (instead of just the statusbar). Neither does the following:
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
But this only seems to work when the viewController is not embedded in a navController (when I hide the navBar, it works!). My viewController hierarchy is the following:
tabBarController -> navigationControllers -> viewController
Also: Setting 'View controller-based status bar appearance' to YES & NO does not make a difference.
I am glad for any help!
You can try following.
keep this to your ViewController
-(UIStatusBarStyle)preferredStatusBarStyle{
// Add If/else conditions based on which style required on which condition
return UIStatusBarStyleLightContent;
}
Call this code when you want to change the status bar style..
[self preferredStatusBarStyle];
[self setNeedsStatusBarAppearanceUpdate];

Runtime Hide / Show Status Bar iOS 9+

I have an application where it is important to hide / show the status bar and switch its style on the fly. Previously, it was very easy with the following calls:
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
But they have been deprecated, and I don't quite understand how the new methods work. I was able to set the style and initial visibility by adding the following line to the plist:
View controller-based status bar appearance = YES
And then adding the following methods to my view controller:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
- (BOOL)prefersStatusBarHidden
{
return NO;
}
This works fine on the view controllers as a whole (as a static setting that gets called when the view is initialized), but I am unable to change them on the fly, which is what I need.
How could I achieve this?
I hate to answer my own question, but after doing some digging, I found how to call the method manually. First, I created a BOOL variable that can be switched on the fly and then returned in the prefersStatusBarHidden method.
- (BOOL)prefersStatusBarHidden
{
return isStatusBarHidden;
}
Then, whenever I wanted to hide/show the status bar, I changed the value of isStatusBarHidden and forced the view to check if its staus bar needs to be updated like so:
isStatusBarHidden = NO;
[self setNeedsStatusBarAppearanceUpdate];
Works perfectly for me on devices running iOS9 and above.

UIStatusBar will not go away. Sadness and frustration ensues

Tried everything. Just trying to hide it for one view controller.
.plist:
Status bar is initially hidden = NO
View controller-based status bar appearance = YES
view controller:
- (BOOL)prefersStatusBarHidden {
return YES;
}
//I shouldn't have to do this, the above method should suffice. Doesn't work anyway
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
Nothing works. Status bar is still there, staring me in the face, laughing through it's ugliness. What do I need to do???
EDIT: prefersStatusBarHidden does not even get called. This view controller is pushed onto the navigation stack via push segue.
In any custom containing view controller, implement childViewControllerForStatusBarHidden, returning the current child view controller that should controller the status bar appearance (in this case, the navigation controller).
This will let the system follow the view controller hierarchy down to the current "top" view controller, and it's that view controller's prefersStatusBarHidden which will be queried.
In your custom containing view controller, if the current "active" child view controller changes, call setNeedsStatusBarAppearanceUpdate to let the system know.
The key here was that this was never getting called in the view controller:
- (BOOL)prefersStatusBarHidden {
return YES;
}
Tracing backward, it was not called in the parent navigation controller either. This is because the nav controller was owned by a root view controller. The root view controller did call this method, but did not pass it on to the nav controller, and thus not to any other child view controllers. So for anyone having the same issue and trying to debug, try to track down the view controller at the "root" of your hierarchy.
So in my case, I post a notification from any view controller (viewWillAppear) that I want to hide the status bar. This notification is then consumed by the root controller:
- (void)hideStatusBar:(NSNotification *)notification {
self.hideStatusBar = YES;
[self setNeedsStatusBarAppearanceUpdate];
}
Which forces this method to be called on the root controller:
- (BOOL)prefersStatusBarHidden {
return self.hideStatusBar;
}
And everything works as expected. The same can be done for showing the status bar again.

Status bar in single view hide/ style

in my app I've multiple views . I need to hide the status bar for one view ( this vC2 is in order navigationC -> VC ->push to vC2) . it works firstly by setting
View controller-based status bar appearance = NO in plist
and then use
[UIApplication sharedApplication].statusBarHidden = YES; in the viewWillAppear
and [UIApplication sharedApplication].statusBarHidden = NO; in viewWillDisappear
to reback the status bar.
Then , I want to set the status bar with light colour style for another single view (VC3) in the same app. only one way works with me is
by setting View controller-based status bar appearance = YES and use
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
in the VC3.m
my problem is around View controller-based status bar appearance which used with NO value for hiding status bar and used with YES value for light style of status bar
Can I combine the the hiding / and style status bar in my app?
My target is iOS7
YOu can by using this function in viewController:
- (BOOL)prefersStatusBarHidden {
return YES;
}
The plist boolean has to be YES and you can add something more:
Try
[self setNeedsStatusBarAppearanceUpdate]
And in case you have view controllers as childs of other viewControllers, the last child is the one that should decide
If the VC is child of another VC (this on the first level VC that you subclass, not need if you are using a navigation without subclassing)
- (UIViewController *)childViewControllerForStatusBarHidden {
return _myChildViewController;
}

How to hide status bar in iOS 7?

I have tried setting the following in my app .plist file:
View controller-based status bar appearance: NO
And while this removes it from my initial view controller, once I go to another view and come back with my navigation controller, it comes right back and this time it does not disappear. Also, I don't see why it would matter but I have also set the status bar under simulated metrics to "None" but that doesn't seem to help. I know i am going to have the navigation bar but the status bar I need gone.
How can I get this done? Please provide a detailed answer, sample code would be great!
Update: This is NOT a duplicate solution as I have tried all other solutions and NONE seem to work for me. Most recently I tried
[[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
Again, with no results. When the app initially launches a status bar is NOT present, after the user visits another view, the status bar is now present in the 2 and other views and does not go away. Even if you go back to the main view.
I have tried all of the suggestions that were posted here, unfortunately what happened here was a small mistake, in my viewDidLoad I had:
[[UIApplication sharedApplication] setStatusBarHidden:YES];
But in my viewWillAppear I had:
[[UIApplication sharedApplication] setStatusBarHidden:NO];
So this was just an issue of overriding, problem fixed now.
To hide status bar:
if [View controller-based status bar appearance: NO]: in AppDelegate.m call
[[UIApplication sharedApplication]setStatusBarHidden:YES];
else: in every view controller
- (BOOL)prefersStatusBarHidden
{
return YES;
}
Try this 2 steps:
In .Plist file of project set the property:
View controller-based status bar appearance = NO;
and
2.In all view controller's .m file in viewDidLoad method put this line of code:
[[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
Use this method in the View Controller which you'd like the Status Bar hidden:
- (BOOL)prefersStatusBarHidden {
return YES;
}
This should work :
// In iOS7 this gets called and hides the status bar so the view does not go under the top iPhone
// status bar
- (BOOL)prefersStatusBarHidden {
return YES;
}
none of these work for me.
when i try this method i get the message "use of undeclared identifier preferstatusbarHidden
include - (BOOL)prefersStatusBarHidden {
return YES;
}
I don't know what to do anymore. I tried setStatusBarHidden, prefersHiddenStatusBar and still no results. Finally i have went through the below you tube link :
https://www.youtube.com/watch?v=FtpBXdMSqRQ
It worked for me.

Resources