Swift-Status bar color display (font, background)

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;

      such as: The foreground part is white, the background part is black

注意:这里只涉及到ios7以及更高版本,低版本下面的讲解可能无效。

First, set the "foreground part" of StatusBar

Simply put, the color that displays the battery level, the time, the network part,
Only two colors can be set here:

    • Default Black (uistatusbarstyledefault)
    • White (uistatusbarstylelightcontent)

There are two places that can be set: plist settings inside and in the program code

According to the UI to the diagram, because the background color to highlight, so the status bar is required to use bright colors, but in fact the default is black

Two methods are used here:

The first kind: you're going to be the whole project. The status bar turns white:

Operation Appdelegate

1) Added view-controller-based status bar Appearance property in Info.plist, value Select No

2) in the Startup method Func application (...) Write down with direct writing

Uiapplication.sharedapplication (). Statusbarstyle = . Lightcontent

This will change the default black to white

Launch the app, complete, once set, all pages are the same. However, there are different requirements for the status bar color, you need to use the 2nd method

The second type: only some controllers need to modify the color of the status bar text:

Operation Viewcontroller

1) is also the operation of the Info.plist, the same as Method 1 new properties, but this time the value is yes

2) in Viewcontroller, you can override the following methods to

Override Func Preferredstatusbarstyle (), Uistatusbarstyle {

return. Lightcontent;

}

Launch app, complete, more flexible operation.

Second, set the "background part" of StatusBar

The background part, simply speaking, is the background color; there are two ways to change it:

System-Provided methods

navigationbar setbartintcolor interface, this interface can change the background color of StatusBar

Note: Once you have set the Navigationbar -(void) SetBackgroundImage: (UIImage *) backgroundimage forbarmetrics: ( uibarmetrics) Barmetrics interface, then the above Setbartintcolor interface can not change the background color StatusBar, statusbar the background color will become pure black.

Another way

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.

Cases:

Info.plist added view-controller-based status bar Appearance property, value Select No

Func Application (application: uiapplication, didfinishlaunchingwithoptions launchoptions: [NSObject : anyobject]?), Bool {

//Override point for customization after application launch.

window = UIWindow(frame: uiscreen. Mainscreen(). Bounds)

window?. Rootviewcontroller = uinavigationcontroller(rootviewcontroller: viewcontroller())

window?. backgroundcolor = uicolor. Whitecolor()

window?. makekeyandvisible()

// status bar font white

uiapplication. Sharedapplication(). Statusbarstyle =. Lightcontent

// status bar background color

Adding a view to the window remains at the forefront, and it is important to note that the view is created after the root view controller is set up, otherwise it will not appear.

let statebarblockview = UIView(frame: cgrectmake(0, 0, window!). bounds. Width, ))

Statebarblockview. backgroundcolor = uicolor. Blackcolor()

window?. addsubview(statebarblockview)

window?. Bringsubviewtofront(statebarblockview)         

return true

}

Swift-Status bar color display (font, background)

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.