iOS设置导航栏透明度
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:
-
E.g. in your ..AppDelegate.m add these lines in didFinishLaunchingWithOptions
-
//create background images for the navigation bar UIImage *gradientImage44 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for portrait orientation UIImage *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:
-
//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):
-
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 tableView滑动设置导航栏透明度渐变 类似QQ空间导航栏渐变
- ios设置导航栏背景图片、返回按钮背景、标题颜色等等
- ios 导航栏设置
- 转:ios导航栏设置
- iOS设置标题栏文字颜色,以及设置导航栏背景颜色、文字颜色
- iOS 8 设置导航栏的背景颜色和背景图片
- iOS设置状态栏和导航栏颜色
- iOS项目开发实战——设置视图的透明度改变动画
- ios设置导航栏的背景颜色或背景图片
- 仿新浪微博IOS客户端(v5.2.8)——设置导航栏外观
- ios学习(导航栏上两个简单的设置)
- iOS开发 设置整个导航栏背景图、字体及标签栏更改选中背景图的颜色
- iOS 设置父视图的透明度而不影响子视图的透明度
- [iOS微博项目 - 1.1] - 设置导航栏主题(统一样式)
- iOS设置子视图透明度不受父视图影响
- ios导航栏的背景图片设置
- iOS 导航栏rgb值与设置的有差异
- ios ios7导航栏返回键设置
- iOS-设置navigation导航栏标题的颜色
- iOS-设置导航栏透明及字体、左右按钮、边框颜色