UINavigationController特性总结(1)

1.translucent属性

translucent是navigationBar的一个属性,当赋值为YES时(默认值),navigationBar为半透明效果.当赋值为NO时,navigationBar为不透明效果.值得注意的是,这个属性会影响到布局.

translucent 显示效果 布局
NO 不透明 起始位置为64
YES 半透明 起始位置为0
translucent
举个栗子
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor orangeColor]];
    
    self.navigationItem.title = @"Hello World";
    self.navigationController.navigationBar.translucent = YES;//默认值为YES(半透明效果)
    
    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
    imageView.image = [UIImage imageNamed:@"hy.jpg"];
    [self.view addSubview:imageView];
}

@end
效果
translucent属性设置为YES,(默认值)

图片的y值为0,布局的起始位置也是0

半透明

translucent属性设置为NO

效果为不透明,图片的y值仍然设置为0,但是布局的起始位置变成了64

不透明


2.设置navigationBar的颜色

第一步:使用Quartz2D将颜色渲染成UIImage
第二步:使用navigationBar的setBackgroundImage:(nullable UIImage *) forBarMetrics:(UIBarMetrics)方法将这个颜色变成的图片设置为navigationBar的背景图片

举个栗子
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor orangeColor]];
    
    self.navigationItem.title = @"Hello World";
    //注释掉这句话,系统会根据背景色是否透明而自动设置这个值
    //self.navigationController.navigationBar.translucent = NO;
    
    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
    imageView.image = [UIImage imageNamed:@"hy.jpg"];
    [self.view addSubview:imageView];
    
    UIColor *color = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    
    //当color的透明度为1时(不透明),translucent自动为NO.当color的透明度小于1时,translucent自动为YES.
    NSLog(@"%d",self.navigationController.navigationBar.translucent);
}
效果和特别说明

特别说明:
不再设置 navigationBar 的translucent属性, 当更改背景颜色的透明度时,系统会根据背景色是否透明而自动设置 translucent的属性值.即当color的透明度为1时(不透明),translucent自动为NO.当color的透明度小于1时,translucent自动为YES.显示效果和布局的其实位置都符合本文第一小节的叙述.
效果:
以下效果1和效果2,代码上只有设置颜色的这一句的alpha有区别(分别为1.0 和0.7)
UIColor *color = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];

效果1
效果2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,022评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,689评论 4 61
  • //// san.m// san//// Created by baipeng on 2017/6/12./...
    seventhboy阅读 271评论 0 0
  • 做个稳妥的人,不要高估两年内的自己,也不要低估十年后的自己。 ​ ​​​ 放弃这一段感情或许真的对自己好一点,三年...
    少书山阅读 228评论 0 0
  • 为了了解朋友们对音乐的感受,邀请了一个朋友写了她对于音乐的认知与感觉... 从小生活在大山脚下的小村庄,任何有关外...
    三羊洋阅读 458评论 0 0

友情链接更多精彩内容