Dynamically change status bar text color - ios

I know I can change the iPhone's status bar text color, using methods described here.
However, my app has different themes, and I need to update the status bar accordingly.
Calling
[self setNeedsStatusBarAppearanceUpdate];
and
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
will obviously not work. However, it needs to be local, as in only for a specific TabBar view.

First, select your Project and in the Genernal tab you will see something like this.
enter image description here
Then set Status Bar Style to Light just like the image.
Second, set View controller-based status bar appearance equal to NO in Info.plist. If you can't find it, just add a new row and set it like the above step.
And then, run your app you will see the Status bar text colour is white. :)

Just for the ones who come here for the title.
You can set a flag and toggle between the status bar text colors like in the code segment given:
-(UIStatusBarStyle)preferredStatusBarStyle {
if (barStyleLight){
return UIStatusBarStyleLightContent;
}
else {
return UIStatusBarStyleDefault;
}
}
Also note that, this preferredStatusBarStyle method is called whenever we call: [self setNeedsStatusBarAppearanceUpdate];
Hope it helps.

Related

iOS 8 light status bar not working

I've set the background color of the main UIView in my view controller to a bluish color.
I've also tried all combinations of the following:
Adding this to the app delegate:
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
Setting View controller status based application to NO and YES
Setting 'Status Bar Style' to Light in the project overview.
I'm seeing black status bar text when I want to see white text.
I'd like to set the style at the application level, not the VC level.
My info.plist:
for all IOS 9+
In your plist file change add/change your table with these 2 lines.
1) View controller-based status bar appearance to NO
2) Status bar style to UIStatusBarStyleLightContent
If you want to change style in running app for any reason use this
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
this saved my life numerous times! :-)
Status bar style is determined (by default) at the level of the view controller, not the application. Implement preferredStatusBarStyle in your view controller.
class ViewController : UIViewController {
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
You can determine status bar style at the application level, but to do so, you must throw a switch in your Info.plist. See the docs:
To opt out of the view controller-based status bar appearance behavior, you must add the UIViewControllerBasedStatusBarAppearance key with a value of NO to your app’s Info.plist file, but doing so is not recommended [my italics, and I don't even know whether this is even supported any longer].
Swift 4
In Info.plist add this property
View controller-based status bar appearance to NO
and after that in AppDelegate inside the didFinishLaunchingWithOptions add these code
UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = .lightContent
you can use this code
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
might be Solve this issue.

White transparent toolbar, how do I make status bar match

Using xcode6, and storyboarding
I have a toolbar that is at the top of my view. under the toolbar is a mapview. The map view extends under the status bar which is what I want. Basically I want the toolbar and status bar to be a little transparent ~20%, such that you can see the mapview beneath.
I have setup my toolbar's background color to be white 80% opaque. However I have no idea how to get the status bar to be exactly the same thing.
From everything I know, the status bar is 100% transparent, meaning it will show any view that is beneath it. I have tried to add a 20 point tall view above my toolbar, and I have set that to white, 80% transparent. That almost achieves the same effect that I am going for, but there is a black line between the toolbar and the status bar,
Am I even on the right track? How do I make the toolbar color and transparency match the status bar color and transparency and avoid the line?
EDIT:
clipsToBounds worked great at removing the line.
I added a button programmatically:
-(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)button forPopoverController:(UIPopoverController *) pc {
button.image = [UIImage imageNamed:#"bars"];
NSMutableArray *items = [self.toolbar.items mutableCopy];
if (!items) {
items = [NSMutableArray arrayWithObject:button];
} else {
[items insertObject:button atIndex:0];
}
[self.toolbar setItems:items];
}
And after I add that button my toolbar background turns back to white:
EDIT 2
So I just discovered the UIBarPositionTopAttached enum and that I can set my view as the toolbars delegate. I did this:
(UIBarPosition)positionForBar:(id )bar {
return UIBarPositionTopAttached;
}
And verified that it was called. I was hoping this would tell iOS that my toolbar is attached to the status bar and that my toolbar color and all would flow into the status bar. Is that not the case either?
Seems like attaching a toolbar to the status bar such that the status bar takes the backgroud of the toolbar should not be this hard, i.e. do I really have to create an extra 20 point high view to achieve this? And even if so seems like I am doing it wrong, as when I add button my toolbar transparency goes back to opaque.
The UIToolbar has a hairline shadow at the top. If you set the bounds clipping property it will hide the line.
toolbar.clipsToBounds = YES;
While clips to bounds works great, I think the right answer to this question use barPosition on toolbar.
There are a couple ways to do this:
Set your view as the delegate to the toolbar and implement this delegate method:
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
return UIBarPositionTopAttached;
}
Or in a storyboard set a User Defined Runtime Attribute on the UIToolbar:
barPosition Number 3

Top bar color when keyboard is dismissed during search

I have light top bar colors in my app, because the navigation bar is dark brown:
When I tap search (Keresés here), the following code sets the top bar color to dark content:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
So it looks like this:
But when I type something into the field, then scroll the results list, the keyboard dismisses and the top bar is reverted to white. How can I prevent this?
In iOS 7, the status bar style is, by default, determined by the return value of the UIViewController method -preferredStatusBarStyle. The default implementation of this method returns UIStatusBarStyleDefault.
Setting the info.plist key UIViewControllerBasedStatusBarAppearance to the value of NO returns to the pre-iOS 7 style of taking the status bar appearance from the shared UIApplication object
To fix this you have two options:
1) Implement the -preferredStatusBarStyle method in all your view controllers that want the status bar style to be non-default
2) Add the key "View controller-based status bar appearance" (UIViewControllerBasedStatusBarAppearance) to your info.plist file and set its value to NO
The solution is in this comment: https://stackoverflow.com/a/19513714/511878
Summary: UINavigationController does not forward -preferredStatusBarStyle to it's children.

Under iOS 7, how do I hide and show status bar on the fly (whenever I want to)

Say a user is in a View Controller and wants to enter a "full screen" type mode where the status bar is hidden, under iOS 6 it was a simple method call to hide/show the status bar, but no matter what it seems to persist under iOS 7.
I've seen solutions like this:
- (BOOL)prefersStatusBarHidden {
return YES;
}
But that doesn't allow it to be toggled at runtime. (It doesn't accept any arguments.)
In my info.plist I have View controller-based status bar appearance set to NO.
I'm at wits end. How do I hide it?
Swift 4
show:
(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = false
hide:
(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = true
Objective-c
Well here's one way of doing this:
in myViewController.h
#interface myViewController : UIViewController {
BOOL shouldHideStatusBar;
}
Then in myViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
shouldHideStatusBar = YES;
}
- (BOOL)prefersStatusBarHidden {
return shouldHideStatusBar;
}
and let's say when I touch the screen it should show the status bar now. You'll need to call: setNeedsStatusBarAppearanceUpdate specifically to get this working and then a switch (bool in this case) to show/hide.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
shouldHideStatusBar = (shouldHideStatusBar)? NO: YES;
[self setNeedsStatusBarAppearanceUpdate];
}
setNeedsStatusBarAppearanceUpdate
This should be called whenever the return values for the view
controller's status bar attributes have changed. If it is called from
within an animation block, the changes will be animated along with the
rest of the animation block.
prefersStatusBarHidden:
Return Value A Boolean value of YES specifies the status bar should be
hidden. Default value is NO.
Discussion If you change the return value for this method, call the
setNeedsStatusBarAppearanceUpdate method.
To specify that a child view controller should control preferred
status bar hidden/unhidden state, implement the
childViewControllerForStatusBarHidden method.
If you plan on your app working with iOS 6 as well might want to look at this post
I was having trouble with some of the other answers in iOS 8 so I did a little more research and found: [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];. You can then enable/disable the animation.
Recommendation
For iOS7 support, I'd suggest you turn View controller-based status bar appearance back to YES. This will allow you to control the status bars in code. The iOS transition guide (link) provides other options for managing the status bar appearance (there is not just a single fix here but a number of settings that will ensure you get what you want). Also be aware that even if you set the appearance in code you will want to update your storyboards or nib files to match your default appearance (otherwise you may see the status bar flash temporarily depending on where you set the code to update the status bar). I would recommend that you set the code before the view appears.
After setting your plist property to YES: Be sure this method exists in UIViewController where you want to status bar to disappear:
- (BOOL)prefersStatusBarHidden
{
return YES;
}
Showing the Status Bar
Return No if you'd like the status bar to appear in each of your view controllers.
There can be a number of other status bar related issues:
Preventing the Status Bar from Covering Your Views
Views incorrectly draw underneath the status bar
Alternative Approach
According to Apple's Documentation you can use another method of managing the status bar by leveraging the UIApplication method noted in the reference below (link). setStatusBarHidden is still viable when using this approach.
This option will allow you to continue to use the UIApplication class properties if you follow the plist setting above.
Samples
You can find code samples of the status bar alternative option mentioned above in:
AVPlayerDemo
GLPaint
StitchedStreamPlayer
If the view controller that is on screen is the root view controller then you should just be able to implement the function
- (BOOL)prefersStatusBarHidden {
return _showStatusBar;
}
with _showStatusBar being a BOOL, then whenever you change that property call [self setNeedsStatusBarAppearanceUpdate]
If the view controller is being held inside of something else i.e. a UINavigationController then you need to implement the method - (UIViewController *)childViewControllerForStatusBarHidden on the parent controller first and return the currently presented view controller instance.
This is all with View controller-based status bar appearance set to YES
i think this will work under summary page of your app or else reply me
visibility ---check that box

How to change statusbar tint color with hidden navigationcontroller in iOS 6.0?

I've uinavigationcontroller and within uiviewcontroller. Now my navigationbarhidden = yes.
But when I hide the navigation bar then changed the color of the status bar.
I want to change the color of the status bar as color of the hidden navigation bar.
I did so :
I haven`t used navigation controller anywhere as shown in the picture. This is just hack way in iOS6, but it only changed color for views which are not in a navigation controller. For me though, solution is important only in iOS6
Any idea?
Change StatusBar color
For Hide navigationBar
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
As has been answered in this post:
In your "Project Summary", in "Status Bar", set "Style" and "Tinting" to "Default.
Then, jump into you xib or storyboard and add a UINavigationBar just bellow the status bar.
Set the UINavigationBar "Style" to "Default" and select the "Tinting" of your choice.
Run! :-)
If you don't want any UINavigationBar visible in your Interface, all you want to do is putting the UINavigationBar behind all the Objects, or set the "Alpha" to zero.

Resources