iOS automaticallyAdjustsScrollViewInsets和translucent的詳解

iOS開發也有段時間了,但是對automaticallyAdjustsScrollViewInsets和translucent混合使用還是有一些不清晰,今天我帶大家一起來實踐、學習,有疑問請回復哦。

    private func hideNavigationBar(showBgImage: Bool) {
//        automaticallyAdjustsScrollViewInsets = false    
//        navigationController?.navigationBar.translucent = true
//        navigationController?.tabBarController?.tabBar.translucent = false
    }
下面我說一下demo的主體:UITabBarController上有四個item,子控制器都是用UINavigationController做容器,我在其中一個裏面添加了UITableView,設置frame和父View的一直現在我們開始測試

什麼都不設置(兩個屬性都使用默認)

automaticallyAdjustsScrollViewInsets默認是true打開,

navigationController?.navigationBar.translucent在iOS7之前默認是false,在iOS7以及之後是默認true

log:
window.frame:(0.0, 0.0, 414.0, 736.0)
view.frame:(0.0, 0.0, 414.0, 736.0)
tableView.frame:(0.0, 0.0, 414.0, 736.0)

說明控制器的起始原點是在導航條左下方(0, 64.0)、終點實在tabBar的右上方(414.0, 687.0)。這個時候滾動視圖會發現tableView的高度沒有672.0是完美適配的,tableView的高度相當於(總高度-導航條高度-tabbar高度)這個就是automaticallyAdjustsScrollViewInsets自動適配的效果

automaticallyAdjustsScrollViewInsets設置false

我們關閉自動適配
    private func hideNavigationBar(showBgImage: Bool) {
        automaticallyAdjustsScrollViewInsets = false    
//        navigationController?.navigationBar.translucent = true
//        navigationController?.tabBarController?.tabBar.translucent = false
    }
log:
window.frame:(0.0, 0.0, 414.0, 736.0)
view.frame:(0.0, 0.0, 414.0, 736.0)
tableView.frame:(0.0, 0.0, 414.0, 736.0)

他們的實際frame值都沒有改變但是顯示出的UI卻不一樣,tableView不在是完美適配,會出現cell展示不全的情況,tableView的一部分和底部tabBar重疊。

translucent

automaticallyAdjustsScrollViewInsets設置成false
private func hideNavigationBar(showBgImage: Bool) {
        automaticallyAdjustsScrollViewInsets = false    
        navigationController?.navigationBar.translucent = true
//        navigationController?.tabBarController?.tabBar.translucent = false
    }

log:

window.frame:(0.0, 0.0, 414.0, 736.0)
view.frame:(0.0, 0.0, 414.0, 736.0)
tableView.frame:(0.0, 0.0, 414.0, 736.0)
    private func hideNavigationBar(showBgImage: Bool) {
        automaticallyAdjustsScrollViewInsets = false
        navigationController?.navigationBar.translucent = true
        navigationController?.tabBarController?.tabBar.translucent = false
    }
log:
window.frame:(0.0, 0.0, 414.0, 736.0)
view.frame:(0.0, 0.0, 414.0, 687.0)
tableView.frame:(0.0, 0.0, 414.0, 687.0)
translucent在iOS7之前默認爲false,iOS7以及之後默認爲true,這個屬性有兩個功能:1設置導航條(nav,tabbar)爲半透明狀態;2.修改當前控制器根容器下的屏幕起始原點
translucent爲true則爲半透明狀態,並且控制器原點變成該bar的原點,反之則恢復。

automaticallyAdjustsScrollViewInsets

蘋果在發佈iOS7的時候在控制器(ViewController)類新增了automaticallyAdjustsScrollViewInsets屬性默認是true,字面意思是自動適配滾動視圖(UIScrollView及子類)。
根據上面的例子說明在automaticallyAdjustsScrollViewInsets打開的時候,就算scrollview的frame設置的有問題也可以完成適配。

總結

這兩個屬性建議大家不要一起使用,如果同時使用automaticallyAdjustsScrollViewInsets的優先級高於translucent。



發佈了42 篇原創文章 · 獲贊 9 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章