您的位置:首页 > 移动开发 > IOS开发

iOS automaticallyAdjustsScrollViewInsets和translucent的详解

2016-08-24 15:17 369 查看
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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐