ios – 更改导航栏的alpha值

ios – 更改导航栏的alpha值,第1张

概述这可能吗? 我想在我的视图控制器(动画)中更改导航栏的Alpha值,但是如果我做self.navigationController.navigationBar.alpha = 0.0;则导航栏所占用的屏幕部分完全消失,并留下黑盒子,这不是我想要的(我喜欢它是self.view的背景颜色). 当我支持Colin的答案时,我想给你一个额外的提示来定制包含alpha的UINavigationBar的外观 这可能吗?

我想在我的视图控制器(动画)中更改导航栏的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 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];

>实现方便的方法以编程方式创建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值所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1111831.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存