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。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值