iOS navigation bar background transparent gradient

Source: Internet
Author: User

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

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.