一. 初识translucent
二. 官档优先
- Discussion部分大意如下:
- 默认值为
YES
. 在navigation bar
有一个自定义的background image
的情况下, 如果这个background image
的任何一个像素的alpha
值小于1.0, 则 translucent 属性值为 YES, 反之为 NO;- 若将此属性置为YES(在
navigation bar
具有opaque
的自定义background image
的情况下), 此navigation bar
将会把系统定义的一个小于1.0的透明度应用在这张background image
上.- 若将此属性置为
NO
(在此navigation bar
具有一个translucent
自定义的background image
的情况下), 此navigation bar
会为此图片提供一个opaque
的background
(①若此navigation bar
的样式为UIBarStyleBlack
, 则此background
为黑色; ②若此navigation bar
的样式为UIBarStyleDefault
, 则此background
为白色; ③若navigation bar
的barTintColor属性的值被定义, 则为此颜色;)
三. 文档注释
- iOS 7 新的行为如下:
- 默认值为 YES;
- 您可以通过将属性设置为NO来强制使用不透明背景。(实际情况是设为NO, 导航栏变为白色, 控制器的根视图向下偏移一个导航栏高度的距离, 效果图如1-0所示);
- 如果导航栏具有自定义背景图像,则默认值是从图像的Alpha值推断出来的,如果它具有alpha <1.0的任何像素, 则推断为YES;
- 如果将
setTranslucent:YES
发送到具有不透明自定义背景图像的条形图,则会将小于1.0的系统不透明度(经过推断, 应该为0.8)应用于图像。- 如果您将
setTranslucent:NO
发送到具有半透明自定义背景图像的条形图,它将使用条形图的barTintColor
(如果已定义)为图像提供不透明背景,如果barTintColor为nil,则为UIBarStyleBlack
提供黑色或UIBarStyleDefault
为白色(iOS 12上, 如此设置, 导航栏变为不透明的白色, 推断iOS 12 上的导航栏具有半透明自定义背景图像)。
- iOS 6及更早版本的默认值为NO。 如果barStyle设置为UIBarStyleBlackTranslucent,则始终为YES.
四. 插图1-0
- 系统默认样式
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"地表最强iOS小队";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:nil];
self.view.backgroundColor = [UIColor magentaColor];
}
- 设置translucent为NO的样式
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.title = @"地表最强iOS小队";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:nil];
self.view.backgroundColor = [UIColor magentaColor];
}
Others
- 测试环境为
iOS 12
;