UINavigationController 的表的偏移情况总结
2018-07-17 本文已影响49人
Mr大喵喵
在iOS中UINavigationController 导航是不可或缺的,而在导航的使用中,由于不经意的一些操作,经常会引起一些界面的变化。
![](https://img.haomeiwen.com/i2419271/9fb98dbd0b0032a0.jpg)
- 我们进入代码实验
首先完全无添加的代码
class TestNavVC: UIViewController {
lazy var themeTab: UITableView = { [weak self] in
let tab = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: .plain)
tab.separatorStyle = .singleLine
tab.rowHeight = 200
tab.dataSource = self
tab.delegate = self
return tab
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(themeTab)
}
}
extension TestNavVC: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 19
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
-
效果图
WechatIMG2.jpeg
O(∩_∩)O哈哈~ 毫无不妥之处
但是
当升级到iOS 11的时候,发现UIScrollView 有莫名其妙的偏移了
所以一般我们会这样设置
- 请看升级代码
class TestNavVC: UIViewController {
lazy var themeTab: UITableView = { [weak self] in
let tab = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: .plain)
tab.separatorStyle = .singleLine
tab.rowHeight = 200
tab.dataSource = self
tab.delegate = self
return tab
}()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "我是导航"
// 解决ios11 由于安全区域safeArea问题造成的表的向下偏移
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
view.addSubview(themeTab)
}
}
extension TestNavVC: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 19
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
看效果
![](https://img.haomeiwen.com/i2419271/e7a09c3334b2e810.jpeg)
what? 可以看出来明显的偏移了。
顿时感觉被![](https://img.haomeiwen.com/i2419271/eb5546d25417a325.jpg)
然后我们再加一手代码
class TestNavVC: UIViewController {
lazy var themeTab: UITableView = { [weak self] in
let tab = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: .plain)
tab.separatorStyle = .singleLine
tab.rowHeight = 200
tab.dataSource = self
tab.delegate = self
return tab
}()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "我是导航"
// 解决ios11 由于安全区域safeArea问题造成的表的向下偏移
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
// 给导航加一张背景图
navigationController?.navigationBar.setBackgroundImage(UIImage(named: "nav_bg"), for: .default)
view.addSubview(themeTab)
}
}
extension TestNavVC: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 19
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
看效果
![](https://img.haomeiwen.com/i2419271/65c460d051faa06c.jpeg)
(⊙o⊙)… 又好了
![](https://img.haomeiwen.com/i2419271/c43dd09cff973de8.jpg)