I have some problems in XCode. I tried to get my navigation bar colored which works perfectly but, the statusbar won't change its color too. I already searched, but nothing works for me.
I hope it is possible to do this without code.
Here is my take on this:
First, iOS gives only two kinds of status bar appearance - light or default.
It is important to note as well that the system status bar no longer
has any background color. When the API refers to
UIStatusBarStyleLightContent, they mean white text on a clear
background. UIStatusBarStyleDefault is black text on a clear
background.
To set status bar white throughout the app:
UIApplication.sharedApplication().statusBarStyle = .LightContent
To set status bar black throughout the app:
UIApplication.sharedApplication().statusBarStyle = .default
Second, As a side note, in your info.plist, you can use UIViewControllerBasedStatusBarAppearance to NOif you want to set the above style at app level (inside your AppDelegate). Otherwise set this property to YES and implement following method in each your view controller to set different styles based on your needs:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
Third, To have a custom color on your status bar, you can add a UIView of desired colour just below the status bar like this:
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, screenWidth, 20);
addStatusBar.backgroundColor = [UIColor grayColor]]; //assign here your color
[self.window.rootViewController.view addSubview:addStatusBar];
Fourth, importantly, UINavigationController will alter the height of its UINavigationBar to either 44 points or 64 points based on following points:
If the UINavigationController detects that the top of its view’s
frame is visually contiguous with its UIWindow’s top, then it draws
its navigation bar with a height of 64 points.
If its view’s top is not contiguous with the UIWindow’s top (even if
off by only one point), then it draws its navigation bar in the
“traditional” way with a height of 44 points.
Related
I wanted to get a specific colour to the iOS status bar ,I understand it will take the colour of the top view controllers view .
And hence I can get this by applying the colour I want for the status bar to the table view ,which would look like this
But when it doesn't have any rows then whole table view gets that color (Obvious! but thats not what i want)
Is there any way to give colour only to the top part of the tableview so that status bar gets that colour or can i change the status bar color alone .
I have
View controller-based status bar appearance
in Info.plist as NO .
You can apply background color to status bar like following
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:#"statusBarWindow"] valueForKey:#"statusBar"];
if ([statusBar respondsToSelector:#selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor redColor];
}
Using the above code in AppDelegate didFinishLaunchingWithOptions method would change the status bar background color of entire app. If you want to change the background color to specific view controllers, then you should follow the #Lion's approach.
Hope this helps.
you should take one custom view in place of status bar and give background color to that view. and take your tableview just below this custom view so you not need to set color on tableview's upper part!!
View's height should be 20 and width should equal to screen width.
and position should be (0,0).
Hope this will help :)
You can also add one window to top status bar and change its color. It will look like this:
// AppDelegate.Swift
let topWindow = UIWindow(frame: CGRect(....))
topWindow.backgroundColor = ...
window.addSubview(topWindow)
On Swift:
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
UIApplication.sharedApplication().statusBarHidden = false
UIApplication.sharedApplication().statusBarStyle = .LightContent
let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
if statusBar.respondsToSelector("setBackgroundColor:") {
statusBar.backgroundColor = UIColor.redColor()
}
}
This question already has answers here:
Set the status bar to black colour
(5 answers)
Closed 6 years ago.
I want to set a background color like Facebook app's statusbar, but I can't find a way to fix it in Apple's documents. Is there any advice for me?
You cannot change the status bar color, as it has not background to change.
However you could create a view on 20px height and place it behind the status bar.
If you have a UINavigationBar, you could either remove the background color of a UINavigationBar or add a subview to the UINavigationBar with a height on 20px and give it the color of the status bar.
I found this on github, you can inspect the code and find out how to change the color for the top 20px https://github.com/remirobert/RRMaterialNavigationBar
As they say, you can generate a 20px view with the color you want.
First you'll need to hide the Status bar in the info.plist:
- Set View controller-based status bar appearance to NO
Then you can set the new color with something like this in the AppDelegate:
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
view.backgroundColor=[UIColor blackColor];
[self.window.rootViewController.view addSubview:view];
One solution consist in these two lines of code in your applicationDidFinishLaunching, in appDelegate:
UIApplication.sharedApplication().keyWindow?.backgroundColor = UIColor.greenColor()
UINavigationBar.appearance().clipsToBounds = true
This will basically change the statusBar background color for all of your app's viewControllers, you have to set also the background color to clearColor() for all of your viewControllers.
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
I would like my status bar to be black and be 25% transparent. I understand that the status bar is transparent by default, and therefore takes on the color of the background. However when I set the views backgroundColor:
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.75f];
The status bar is completely black.
I have a toolbar which I set to black with an alpha of .75 and I am just trying to get them to match:
self.toolBar.tintColor = [UIColor whiteColor];
self.toolBar.barTintColor = [UIColor blackColor];
self.toolBar.alpha = .75f;
Any reason why the background color on the UIView is not respecting the alpha component?
EDIT:
Based on comment that view does not underlap the status bar. If I set background to green it shows that it works:
However if I start to add transparency to the green color, it does not get lighter it gets darker. Seems like the default is to be black underneath my only UIView, not white.
self.view.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:.25];
The status bar in iOS 7 is completely transparent. The problem may be that your view and the toolbar are not correctly underlapping the status bar. So you are seeing the black window behind it. (Or, in fact, you may setting the size of the window incorrectly, in which case you are seeing nothing at all behind the status bar.)
If the view does underlap the status bar, then you need to set the bar positioning of the toolbar to Top Attached so that its height increases up underneath the status bar. We should not be seeing a separate color for the status bar; it should appear to overlay your interface, lying in front of the top of the toolbar.
I want to make an app, which uses a navigationbar, but just for showing the title and some buttons (it is using a vie container). I want the navigation bar to "fusion" with the status bar, like this
But when I use a generic view controller and drag a navigation bar in it just looks as this:
Is there a way to do this?
I've found a solution for this specific problem. Set the navigation bar's translucent property to NO:
self.navigationController.navigationBar.translucent = NO;
UINavigationController will alter the height of its UINavigationBar to either 44 points or 64 points, depending on a rather strange and undocumented set of constraints. If the UINavigationController detects that the top of its view’s frame is visually contiguous with its UIWindow’s top, then it draws its navigation bar with a height of 64 points. If its view’s top is not contiguous with the UIWindow’s top (even if off by only one point), then it draws its navigation bar in the “traditional” way with a height of 44 points. This logic is performed by UINavigationController even if it is several children down inside the view controller hierarchy of your application. There is no way to prevent this behavior.
Full explanation here.
Not sure whether this will fix your issue, but you can try what i did.
I had the same problem in viewcontroller's right side menu "Simulated Metics" > Top Bar > "Opaque Navigation Bar"
viewcontroller.h file in create iboutlet of the navigation bar
#property (weak, nonatomic) IBOutlet UINavigationBar *navBar;
You can do as followings in viewDidLoad
UIView *addStatusBar = [[UIView alloc] init];
//draw status bar with width of device
addStatusBar.frame = CGRectMake(0, -20, self.view.frame.size.width, 20);
//set the background colour to status bar
addStatusBar.backgroundColor = [UIColor colorWithRed:24.0/255. green:24/255. blue:24.0/255. alpha:1];
[self.view addSubview:addStatusBar];
// add your status bar to your UINavigationBar *navBar which declared in your file .h
[self.navBar addSubview:addStatusBar];
navigationController.navigationBar.clipsToBounds = YES;
In order for UINavigationBar to extend its background under status bar its clipsToBounds must be set to NO (which is the default). Make sure you do not mock around with it.
Solution simple as:-
navigationController.navigationBar.clipsToBounds = NO;
Swift 5
navigationController?.navigationBar.isTranslucent = false