一.背景:在做开发的时候经常不经意就出现导航栏遮挡主视图的情况,之前出现这种情况我会先看看View UI Hierarchy,然后把视图的坐标手动调整,例如把视图的y坐标写成64,即减去状态栏和导航栏的坐标,问题虽然是解决了,但是这样的解决方法存在隐患,至于为什么,待我慢慢说来。
二.基础知识
1.关于坐标系
iOS7之前的坐标系见图一,坐标原点是从导航栏下方开始计算的,iOS7以后的坐标系是铺满全屏幕的(图二),也就是将屏幕的左上角作为坐标原点。
图一 图二
2.edgesForExtendedLayout属性
前面说到了iOS7的坐标系是铺满全屏幕的,它实际上是通过edgesForExtendedLayout属性来实现的,该属性默认的值是UIRectEdgeAll,该属性的取值见下面:
@available(iOS 7.0, *)
public struct UIRectEdge : OptionSet {
public static var top: UIRectEdge { get } //从屏幕的左上角开始计算,但不覆盖tabbar标签栏
public static var left: UIRectEdge { get }
public static var bottom: UIRectEdge { get }//从导航栏下方开始计算,覆盖tabbar标签栏
public static var right: UIRectEdge { get }
public static var all: UIRectEdge { get } //默认值,铺满全屏幕
} ps:它还可以设置为UIRectEdgeNone,即和iOS7以前的坐标系一样,swift3.0没有这个成员变量,应该直接赋值为[] 注意:edgesForExtendedLayout属性只是改变了坐标系原点的位置,并不会改变view的宽度和高度,也就是说无论该属性被设置成哪一个值,控制器父视图的默认frame都是 (0.0,0.0, Screen.Width,Screen .Heiht),所以在使用父视图的frame属性给其他子控件赋值时一定要特别注意。 3.automaticallyAdjustsScrollViewInsets属性
先来看看官方文档怎么说,automaticallyAdjustsScrollViewInsets根据按所在界面的status bar,navigationbar,与tabbar的高度,自动调整scrollview的 inset,设置为no,不让viewController调整,我们自己修改布局即可~。该属性是针对scrollview及其子类的,例如tableView和collectionView,但是该属性只对控制器视图层级中第一个scrollview及其子类起作用,如果视图层级中存在多个scrollview及其子类,官方建议该属性设置为no,此时应该手动设置它的inset
ps:当你发现tableview莫名其妙地向下偏移导航栏的高度时,就是这个属性在作怪,将其设置为no即可
三.例子
1.edgesForExtendedLayout和automaticallyAdjustsScrollViewInsets均为默认值,左边是普通的UIView,右边包含TableView
左边:
@available(iOS 7.0, *)
public struct UIRectEdge : OptionSet {
public static var top: UIRectEdge { get } //从屏幕的左上角开始计算,但不覆盖tabbar标签栏
public static var left: UIRectEdge { get }
public static var bottom: UIRectEdge { get }//从导航栏下方开始计算,覆盖tabbar标签栏
public static var right: UIRectEdge { get }
public static var all: UIRectEdge { get } //默认值,铺满全屏幕
}
右边:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.backgroundColor=UIColor.blue
print(tableView.frame)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell=UITableViewCell(style: .default, reuseIdentifier: "cell")
}
cell?.textLabel?.text="测试"
return cell!
}
ps:虽然tableview的内容没有被遮挡,但是它还是铺满全屏幕的,从导航栏的透明度就可以看出来,下面是它的视图层级

2 .edgesForExtendedLayout均为UIRectEdgeNone,左边是普通的UIView,右边包含TableView

ps:这里因为没有tabbar,所以下面是铺满的


3.edgesForExtendedLayout为默认,automaticallyAdjustsScrollViewInsets为no,只看TableView


4.edgesForExtendedLayout为UIRectEdgeNone,automaticallyAdjustsScrollViewInsets为no,只看TableView


ps:当tableview的frame在导航栏下方时,automaticallyAdjustsScrollViewInsets属性就没有什么意义
5.多个tableview,edgesForExtendedLayout和 automaticallyAdjustsScrollViewInsets均为默认值 override func viewDidLoad() {
super.viewDidLoad()
let tableviewVC = TestTableViewController()
let tableviewVC1 = Test1TableViewController()
tableviewVC.tableView.frame=CGRect(x: 0, y: 0, width: 100, height: view.frame.height)
tableviewVC1.tableView.frame=CGRect(x: 100, y: 0, width: UIScreen.main.bounds.width-100, height: view.frame.height)
view.addSubview(tableviewVC.tableView)
view.addSubview(tableviewVC1.tableView)
self.addChildViewController(tableviewVC)
self.addChildViewController(tableviewVC1)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
ps:automaticallyAdjustsScrollViewInsets属性失效,两个tableview视图均被遮挡,这个地方有点疑惑,我之前做项目的时候第一个tableview是没被遮挡的,也就是对第一个tableview还是有效的。不管怎样,按照官方文档的说法,当有多个scrollview及其子类的时候,还是应该把该属性设置为no,手动确定它们的位置。上面这种情形的解决方案只需两句代码就搞定:
//解决方案
self.automaticallyAdjustsScrollViewInsets=false
self.edgesForExtendedLayout=[] //如果下面还有tabbar,则应该设置为self.edgesForExtendedLayout=UIRectEdge.bottom,否则tabbar会出现黑条 