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