在iOS中UINavigationController 导航是不可或缺的,而在导航的使用中,由于不经意的一些操作,经常会引起一些界面的变化。
timg.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
}
}
看效果
WechatIMG4.jpegwhat? 可以看出来明显的偏移了。
顿时感觉被 timg1.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
}
}
网友评论