ios – 更改导航栏的alpha值

这可能吗?

我想在我的视图控制器(动画)中更改导航栏的Alpha值,但是如果我做self.navigationController.navigationBar.alpha = 0.0;则导航栏所占用的屏幕部分完全消失,并留下黑盒子,这不是我想要的(我喜欢它是self.view的背景颜色).

解决方法

当我支持Colin的答案时,我想给你一个额外的提示来定制包含alpha的UINavigationBar的外观.

诀窍是为您的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.

以上是来客网为你收集整理的ios – 更改导航栏的alpha值全部内容,希望文章能够帮你解决ios – 更改导航栏的alpha值所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。