前言

当我们设置导航栏的某些属性的时候会导致控制器View的布局不是从window的 (0,0)点开始布局,会从导航栏底部开始布局,而此时在 viewDidLoad 中 获取到View的frame 确实从(0,0)开始的,只有在 viewDidAppear中才能获取到 view 最终的实际 frame

一些属性

在了解 UINavigationBar之前,有必要了解 UINavigationBar 的一些属性
///默认 default 半透明 black 黑色
open var barStyle: UIBarStyle
// 底部阴影横线,默认nil
// 官方解释还涉及到了一个设置背景图片的方法 -setBackgroundImage:forBarMetrics:
open var shadowImage: UIImage?
// 7.0 以后已经改变,修改bar 背景颜色 请使用 -barTintColor
open var tintColor: UIColor!
// default is nil bar 的背景颜色
open var barTintColor: UIColor?
/// 影响比较大的属性见下文,是否是半透明的
open var isTranslucent: Bool // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
一些条件
///当前 控制器并不是 tableviewcontroller
self.view.backgroundColor = .cyan
self.tableView.backgroundColor = .red
self.navigationItem.title = “rootVC 标题”
tableView.frame = view.bounds
1.1 默认导航栏 带有半透明效果

此时view 和 tableview 和 导航栏布局

1 view全屏布局

2 tableview默认从导航栏下部开始布局

3 导航栏半透明

细节 : 此时导航栏中的 _UIVisualEffectBackdropView 属性变成红色即 tableview的背景色
https://zsrimg.ikafan.com/file_images/article/201909/2019912101002272.jpg?2019812101011
https://zsrimg.ikafan.com/file_images/article/201909/2019912101022382.jpg?2019812101028
1.2 此时如果想让tableview 从顶部开始布局可添加代码
if #available(iOS 11.0,*) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never;
} else {
self.automaticallyAdjustsScrollViewInsets = false;
}
神奇的是 如果 tableview从顶部布局 此时导航栏中的 _UIVisualEffectBackdropView 属性又会变成默认白色
https://zsrimg.ikafan.com/file_images/article/201909/2019912101117673.jpg?2019812101123
2 设置导航栏 isTranslucent属性

isTranslucent 在6.0以后默认是 true

如果设置为false
self.navigationController?.navigationBar.isTranslucent = false
此时布局

1 view 从导航栏底部布局

2 tableview 从view (0,0) 布局

3 导航栏不透明 _UIBarBackground 默认为白色
https://zsrimg.ikafan.com/file_images/article/201909/2019912101148475.jpg?2019812101153https://zsrimg.ikafan.com/file_images/article/201909/2019912101148475.jpg?2019812101153
https://zsrimg.ikafan.com/file_images/article/201909/2019912101200418.jpg?201981210126
3.1设置barTintColor
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.barTintColor = UIColor.purple
此时布局和默认一样

1 view从 (0,0)布局

2 tableview从导航栏底部布局
3 导航栏半透明

不同的是 UIVisualEffectView多加了一个 _UIVisualEffectSubview 用来显示我们自定义的背景色

其他两个 _UIVisualEffectSubview 和 _UIVisualEffectBackdropView view 用来实现半透明效果
https://zsrimg.ikafan.com/file_images/article/201909/2019912101243510.jpg?2019812101249
https://zsrimg.ikafan.com/file_images/article/201909/2019912101257001.jpg?201981210133
3.2在 barTintColor基础上设置 isTranslucent = false 属性

结果 和 2 中的效果一样。不同的是

_UIBarBackground 变成了我们自定义的颜色
4.1 设置 setBackgroundImage

设置一张纯色图片
self.navigationBar.setBackgroundImage(UIColor.mm_colorImgHex(color_vaule: hex,alpha: 1), for: UIBarPosition.any, barMetrics: .default)
此时 布局

1 view 从导航栏底部布局 view—-(0.0, 88.0, 414.0, 808.0)

2 tableview 从(0,0) 布局

3 导航栏不透明

此时打印导航栏 isTranslucent属性 为false也就是说如果调用了setBackgroundImage会默认 将 isTranslucent 置位 false
translate——-Optional(false)
https://zsrimg.ikafan.com/file_images/article/201909/2019912101631497.jpg?2019812101639
https://zsrimg.ikafan.com/file_images/article/201909/2019912101644322.jpg?2019812101650
4.2 我们在4.1的情况下 修改 isTranslucent

在 viewWillAppear 中修改 isTranslucent 为 true

此时布局

1 view 全屏布局

2 tableview从导航栏底部顶部开始布局

3 导航栏透明

此时打印我们的 _UIBarBackground 中的 BackgroundImage 透明度已被修改
<UIImageView: 0x7fbef1f0ce10; frame = (0 0; 414 88); alpha = 0.909804; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x600000cabd00>>

更多相关文章

  1. Android(安卓)官方数据库Room --- 配置
  2. Android(安卓)自定义View (一)
  3. AndroidManifest.xml文件详解(uses-configuration)
  4. JSP JavaBean的setProperty属性
  5. RelativeLayout常用属性介绍
  6. Android(安卓)UI设计技巧
  7. Android自定义属性时TypedArray的使用方法
  8. TabLayout属性及自定义底部导航栏
  9. AndroidManifest.xml文件详解(uses-configuration)

随机推荐

  1. PHP网络请求插件Guzzle使用
  2. 程序员还看带广告的小说?
  3. 使用PHP反射机制获取函数文档
  4. PHP快速搭建一个简单的QQ机器人
  5. PHP实现驼峰命名和下划线命名互转
  6. PHP之你不得不知道的COOKIE含义及使用方
  7. PHP百钱百鸡问题(三种解题思路及答案)
  8. 如何用PHP代码生成金字塔
  9. PHP路由库FastRoute的使用教程
  10. PHP日期时间快速入门(图文详解)