Change the transparency of the status bar and navigation bar

Source: Internet
Author: User

How do you achieve this translucent effect?



Step One:

To add a static method to the UIImage class:

Extension UIImage {        static func Imagewithcolor (Color:uicolor), UIImage {let                rect = CGRect (x:0, y:0, Width : 1, height:1)        Uigraphicsbeginimagecontext (rect.size) let        context = Uigraphicsgetcurrentcontext ()        Cgcontextsetfillcolorwithcolor (context, color. Cgcolor)        cgcontextfillrect (context, rect) let                image = Uigraphicsgetimagefromcurrentimagecontext ()        Uigraphicsendimagecontext ()                return image    }}

The method takes a Uicolor object and then returns a UIImage object of that color.

Step Two:

Create a uicolor of a certain transparency

Let Semiblackcolor = Uicolor (red:0,green:0,blue:0,alpha:0.5)

#000为黑色, this sets the opacity of the black to 0.5.


Step Three:

To set the transparency of the status bar and navigation bar:

Navigationcontroller?. Navigationbar.setbackgroundimage (Uiimage.imagewithcolor (Semiblackcolor), Forbarmetrics:. Default) Navigationcontroller?. Navigationbar.translucent = TRUE//must be set to True


Dynamically changing this transparency can be achieved by uiscrollviewdelegate:

Func Scrollviewdidscroll (scrollview:uiscrollview) {    //listens for Uiscrollview properties Contentoffset.    if Scrollview.contentoffset.y > {        //...    }}

Note: This method is only applicable to iOS7 and above.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Change the transparency of the status bar and navigation bar

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.