Key analysis:
1. Hide the bottom line of the navigation.
2. Set the opacity of the navigation background (ios7.0 above without setting, translucent the default is yes without modification, but if someone else modifies or appears that way, it needs to be set to Yes).
3. When the page disappears, the navigation reverts to the original.
4. When scrolling, call method to set Alpha to control the gradient of the navigation background (also the core, focus here).
The core code is as follows:
1. Convert a color to a picture
-(UIImage *) Imagewithcolor: (Uicolor *) color { //create 1 pixel area and start picture drawing cgrect rect = cgrectmake (0, 0, 1, 1); Uigraphicsbeginimagecontext (rect.size); Create Artboards and populate color and area cgcontextref context = Uigraphicsgetcurrentcontext (); Cgcontextsetfillcolorwithcolor (context, [color Cgcolor]); Cgcontextfillrect (context, rect); Get the picture from the artboard and close the picture drawing UIImage *image = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); return image;
2. Call change navigation gradient when scrolling
-(void) ChangeColor: (Uicolor *) color Withscrollview: (Uiscrollview *) ScrollView Andvalud: (cgfloat) value{/** if ( Scrollview.contentoffset.y < 0) {Carefully Select this, only apply to the fixed navigation bar, if the custom view effect will be different //dropdown navigation hidden Self.hidden = YES; } else{*/ Self.hidden = NO; Calculate transparency CGFloat alpha = scrollview.contentoffset.y/value > 1.0f? 1:scrollview.contentoffset.y/value; Set color to picture UIImage *image = [self Imagewithcolor:[color colorwithalphacomponent:alpha]; [Self setbackgroundimage:image forbarmetrics:uibarmetricsdefault]; Self.translucent = Alpha >=1.0f? No:yes; /* }*/}
3. Hide the line under the navigation bar
-(void) start{ uiimageview *shawimage = [self findnavlineimageon:self]; Shawimage.hidden = YES; Self.translucent = YES;}
4. Reset the page after it disappears
(Applies to navigation bar not applicable to Custom view) Uiimageview *shawimage = [self findnavlineimageon:self]; Shawimage.hidden = NO; [Self Setbackgroundimage:nil forbarmetrics:uibarmetricsdefault]; Self.translucent = NO;
5. Reference article: https://www.jianshu.com/p/10c71cb19b5e
iOS navigation bar background transparent gradient