Several ways IOS changes the status bar, navigation bar color

Source: Internet
Author: User

The status bar on iOS means the top 20-pixel high section.
The status bar is divided into two parts, to distinguish between the two concepts, the following will be used:

Foreground part: Refers to the display of battery, time and other parts;
Background: The background part of the display is black or picture;

(i) Setting the "foreground part" of StatusBar

Simply put, you can set the color of the battery, the time, the network part, and only two colors are set here:

Default Black (Uistatusbarstyledefault)
White (uistatusbarstylelightcontent)
There are two places that can be set: plist settings inside and in the program code
Initialization settings: The navigation bar is set to opaque and the title is compared to the status bar text

                   self.edgesForExtendedLayout = 0;                   self.navigationItem.title = @"标题";

Only set Navigationbar opaque and write a title. png


Ways to change the status bar
Method One:
1, plist
View controller-based status bar appearance set to NO


Set to No.png


2. Code settings

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

The effect is as follows:


Status bar White. png


Method Two:
1, plist
View controller-based status bar appearance set to YES or default (not set)
Attention:
If view controller-based status bar appearance is yes.
The [uiapplication Sharedapplication].statusbarstyle is invalid.
2. Code settings

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

Status bar navigation bar text is white, background black. png

or override the method in the controller preferredStatusBarStyle to modify the status bar color

- (UIStatusBarStyle)preferredStatusBarStyle {//    return UIStatusBarStyleLightContent;    return UIStatusBarStyleDefault;}

(ii) Setting the "background" of the StatusBar
The background part, simply speaking, is the background color; there are two ways to change it:

1, the system provides methods

Navigationbar Setbartintcolor interface, this interface can change the background color of StatusBar

self.navigationController.navigationBar.barTintColor = [UIColor greenColor];

Pure background color setting, the default font is black. png

If you want to turn the status bar and the navigation bar font all white, you can

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

This line of code can change the status bar and the navigation bar font color in all, only black or white. png


If you only want to change the font color of the navigation bar, you can

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];

It is also possible to directly modify the size and color of the file in the title that is displayed by default. png


You can also change the font size

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:25]}];

Change the font color size. png


Or you can set a background picture

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"image01"] forBarMetrics:UIBarMetricsDefault];

Picture background. png


2. Alternative

Create a UIView,
Set the frame.size and StatusBar size of the UIView,
Set the Frame.origin for the UIView to {0,-20},
Set the background color of the UIView to the color of the statusbar you want,
Addsubview the UIView on the Navigationbar.
Principle:
The area of the status bar area relative to the Navigationbar is

{0,-20,self.view.bounds.size.width,20}

In addition to changing the foreground color of the status bar (text color, WiFi color, time color, battery color), it is changing the background color. Because the controls on the status bar area are hidden, the background color of the status bar changes as soon as the color is rendered in the status bar area, changing the background color of the status bar.

UIView *statusBarView = [[UIView alloc]   initWithFrame:CGRectMake(0, -20,    self.view.bounds.size.width, 20)];statusBarView.backgroundColor = [UIColor greenColor];[self.navigationController.navigationBar addSubview:statusBarView];

Change the background color of the status bar. png

In addition picture transparent processing
Navigationbar is transparent, comment out self.edgesforextendedlayout = 0;

// self.edgesForExtendedLayout = 0; [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault] self.navigationController.navigationBar.shadowImage = [UIImage new];

Picture transparency settings. png

Id
Links: http://www.jianshu.com/p/63f758796438
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Several ways IOS changes the status bar, navigation bar color

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.