美文网首页
UINavigationController 的表的偏移情况总结

UINavigationController 的表的偏移情况总结

作者: Mr大喵喵 | 来源:发表于2018-07-17 17:46 被阅读49次
在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.jpeg

what? 可以看出来明显的偏移了。

顿时感觉被 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
    }
    
}

看效果

WechatIMG6 1.jpeg

(⊙o⊙)… 又好了

timg2.jpg
可以看出来,这些代码都会影响到含有表的导航界面的布局情况。简单列举一些,方便记录学习,也希望小伙伴们有更多更好的意见、情景分享学习一下。(#.#)谢谢老铁!

相关文章

网友评论

      本文标题:UINavigationController 的表的偏移情况总结

      本文链接:https://www.haomeiwen.com/subject/xosgpftx.html