iOS Set nav bar transparency

Source: Internet
Author: User

As I support Colin's answer, I want to give a additional hint to customize the appearance of an Uinavigationbar inclu Ding the alpha.

The trick is to use uiappearance for your navigationbar. This enables assign a UIImage to your Navigationbar ' s backgroundimage. You can generate these uiimages programmatically and use for that uicolors and set the colors ' Alpha Properties a s you want. I ' ve done this in one of my own applications and it works as expected.

Here I give you some code snippets:

  1. e.g. in your. APPDELEGATE.M add these lines in Didfinishlaunchingwithoptions

  2. //Create background images for the navigation barUIImage *gradientimage44 = nil;//Replace "nil" with your method to programmatically create a UIImage object with transparent colors for portrait Orient ationUIImage *gradientimage32 = nil;//Replace "nil" with your method to programmatically create a UIImage object with transparent colors for landscape Orien Tation//Customize the appearance of Uinavigationbar[[Uinavigationbar appearance] setbackgroundimage:gradientimage44 Forbarmetrics:uibarmetricsdefault]; [[Uinavigationbar appearance] setbackgroundimage:gradientimage32 Forbarmetrics:uibarmetricslandscapephone]; [[Uinavigationbar appearance] setbarstyle:uibarstyledefault];

    2.Implement convenience methods to programmatically creates UIImage objects, e.g. create a new category for UIImage:

  3. //Uiimage+initwithcolor.h//#import<UIKit/UIKit.h>@interfaceUIImage (Initwithcolor)//programmatically create an UIImage with 1 pixel of a given color+ (UIImage *) Imagewithcolor: (Uicolor *) color;//implement additional methods here to create images with gradients etc.//[..]@end//UIIMAGE+INITWITHCOLOR.M//#import "Uiimage+initwithcolor.h"#import<QuartzCore/QuartzCore.h>@implementationUIImage (Initwithcolor)+ (UIImage *) Imagewithcolor: (Uicolor *) color{CGRect rect= CGRectMake (0,0,1,1); //Create a 1 by 1 pixel contextUigraphicsbeginimagecontextwithoptions (Rect.size, NO,0);    [Color Setfill];    Uirectfill (rect); UIImage*image =Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); returnimage;}

    3.re-work your image creation in 1. (#import "Uiimage+initwithcolor.h" in APPDELEGATE.M and replace the "nil" s):

  4. UIImage *gradientimage44 = [UIImage imagewithcolor:[uicolor colorwithred: 1.0 Green:0.0 Blue:1.0 Alpha:0.2]];             *GRADIENTIMAGE32 = [UIImage imagewithcolor:[uicolor colorwithred: 1.0 Green: 0.0 Blue:1.0 Alpha:0.2]];

    Set transparency with a picture as a background

iOS Set nav bar transparency

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.