Dou*_*ith 14 objective-c uinavigationbar uiview uinavigationcontroller ios
这可能吗?
我想在视图控制器中更改导航栏的alpha值(在动画中),但是如果我这样做self.navigationController.navigationBar.alpha = 0.0;,导航栏占用的屏幕部分完全消失并留下一个黑盒子,这不是我想要的喜欢(我更喜欢它是self.view背景的颜色).
Com*_*ezl 21
当我支持Colin的答案时,我想给你一个额外的提示来定制包含alpha的UINavigationBar的外观.
诀窍是为您的NavigationBar 使用UIAppearance.这使您可以将UIImage分配给NavigationBar的backgroundImage.您可以以编程方式生成这些UIImages并用于该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];
Run Code Online (Sandbox Code Playgroud)实现便捷方法以编程方式创建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, 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;
}
Run Code Online (Sandbox Code Playgroud)在1中重新创建图像创建(在AppDelegate.m中#import"UIImage + initWithColor.h"并替换"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]];
Run Code Online (Sandbox Code Playgroud)
我创建了一个小型演示项目并添加了两个截图:视图本身有一个黄色backgroundColor.NavigationBar的backgroundImages具有红色.屏幕截图1显示了一个NavigationBar,其值为alpha = 0.2.屏幕截图2显示了一个NavigationBar,其值为alpha = 0.8.

最直接的方法是修改navigationBar背景视图的alpha组件,此时(iOS9)是第一个navigationBar子视图.但请注意,我们永远不知道在以后的版本中是否会由apple更改子视图层次结构,所以必须小心.
let navigationBackgroundView = self.navigationController?.navigationBar.subviews.first
navigationBackgroundView?.alpha = 0.7
Run Code Online (Sandbox Code Playgroud)
小智 6
直接来自Apple Developer参考:
"只有直接定制了一把,你可以对导航栏.具体来说,它是正常的修改
barStyle,tintColor和translucent属性,但你绝不能直接改变UIView-电平特性,如frame,bounds,alpha,或hidden直接性."
但是,您可以设置导航栏的半透明属性.如果你这样做[self.navigationController.navigationBar setTranslucent:YES];
应该解决你的问题.您还可以尝试查看是否有任何UIBarStyle您想要的枚举.
| 归档时间: |
|
| 查看次数: |
31958 次 |
| 最近记录: |