IOS (nav bar) NavBar Transparent

Source: Internet
Author: User
Tags uikit

A situation is encountered in real-world development. The UI will make the navigation bar transparent, and when it slides, it becomes visible. It's like this when it's opaque.

It's pretty ugly.

So to make a transparent navigation bar, you need to know a method, a property

At this point, UIImage looks like it has to be a drawing.

-(UIImage *) Imagewithcolor: (Uicolor *) color {    = CGRectMake (0011 );    Uigraphicsbeginimagecontext (rect.size);     = Uigraphicsgetcurrentcontext ();    [Color Setfill];    Cgcontextfillrect (context, rect);     *imgae = uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext ();     return Imgae;}

Of course, you can also add classes to UIImage

Uiimage+colorimge.h

#import <UIKit/UIKit.h>@interface UIImage (colorimge)+ (UIImage *) Imagewithcolor: (Uicolor *) color Andsize: (cgsize) size; @end

Uiimage+colorimge.m

 #import  " uiimage+colorimge.h   "  @implementation UIImage (colorimge)  + (UIImage *) Imagewithcolor: (uicolor *) color andsize: (cgsize) size {cgrect rect  = CGRectMake (0 , 0      Size.width, Size.Height);    Uigraphicsbeginimagecontext (rect.size);    Cgcontextref context  = Uigraphicsgetcurrentcontext ();    Cgcontextsetfillcolorwithcolor (context, [color Cgcolor]);    Cgcontextfillrect (context, rect);    UIImage  *image = Uigraphicsgetimagefromcurrentimagecontext ();    Cgcontextaddellipseinrect (context, rect);    Uigraphicsendimagecontext ();  return   image; @end  

So you can initialize a uiimage directly.

Then we can make the navigation transparent.

Effect

When you want to be able to slide the color gradually similar

Need to be

-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView; The method is a fuss. Because TableView inherits from ScrollView.

In this method, the contentoffset.y of TableView is judged. Look at the code first

Current transparency = (beginning of tableview.contentoffset.y-tableview.contentoffset.y)/(completely opaque end point + tableview.contentoffset.y start) Generally do not make changes to the beginning of the TABLEVIEW.CONTENTOFFSET.Y is 0, the complete opacity of the end is to see your mood sliding how far when the navigation bar full color.

If you have time, you can do the encapsulation. have different effects in different situations

IOS (nav bar) NavBar Transparent

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.