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
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
根據上面的例子說明在automaticallyAdjustsScrollViewInsets打開的時候,就算scrollview的frame設置的有問題也可以完成適配。
總結