iOS設定導覽列透明度

來源:互聯網
上載者:User

標籤:

As I support Colin‘s answer, I want to give you an additional hint to customize the appearance of an UINavigationBar including the alpha.

The trick is to use UIAppearance for your NavigationBar. This enables you to assign an UIImage to your NavigationBar‘s backgroundImage. You can generate these UIImages programmatically and use for that UIColors and set the colors‘ alpha properties as 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 orientationUIImage *gradientImage32 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for landscape orientation//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>@interface UIImage (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>@implementation UIImage (initWithColor)+ (UIImage *)imageWithColor:(UIColor *)color{    CGRect rect = CGRectMake(0, 0, 1, 1);    // create a 1 by 1 pixel context     UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);    [color setFill];    UIRectFill(rect);    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}

    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]];            UIImage *gradientImage32 = [UIImage imageWithColor:[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:0.2]];

    // 用圖片當作背景 設定透明度

iOS設定導覽列透明度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.