我想在我的视图控制器(动画)中更改导航栏的Alpha值,但是如果我做self.navigationController.navigationBar.alpha = 0.0;则导航栏所占用的屏幕部分完全消失,并留下黑盒子,这不是我想要的(我喜欢它是self.view的背景颜色).
解决方法
诀窍是为您的NavigationBar使用UIAppearance.这使您能够将UIImage分配给您的NavigationBar的backgroundImage.您可以以编程方式生成这些UIImage,并为该UIColors使用,并根据需要设置颜色的alpha属性.我已经在我自己的一个应用程序中完成了这项工作,并且按预期工作.
这里我给你一些代码片段:
>例如在你的..AppDelegate.m中添加这些行在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];
>实现方便的方法以编程方式创建UIImage对象,例如为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,1,1); // create a 1 by 1 pixel context UIGraphicsBeginImageContextWithOptions(rect.size,NO,0); [color setFill]; UIRectFill(rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
>重新编辑您的图像创建1.(#import“UIImage initWithColor.h”在AppDelegate.m中,并替换“nil”):
这是您的兴趣点:通过更改颜色的alpha属性,您也影响到您的NavigationBar的不透明度水平!
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]];
我创建了一个小型演示项目,并为您添加了两个截图:视图本身有一个黄色的backgroundColor.NavigationBar的backgroundImages具有红色.屏幕截图1显示了一个值为alpha = 0.2的NavigationBar.屏幕截图2显示了一个值为alpha = 0.8的NavigationBar.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。