iOS11.0之后,原本在VC里面的automaticallyAdjustsScrollViewInsets竟然过期了,在IOS 11下 APPLE推荐使用UIScrollView的contentInsetAdjustmentBehavior属性进行设置自动计算滚动视图的内容边距。
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets
在IOS11的SDK下,UIScrollView的这个属性
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior //这个属性是一个枚举类型的
{
UIScrollViewContentInsetAdjustmentAutomatic,//scrollView会自动计算和适应顶部和底部的内边距并且在scrollView 不可滚动时,也会设置内边距.
UIScrollViewContentInsetAdjustmentScrollableAxes, //自动适应边距
UIScrollViewContentInsetAdjustmentNever, //和 automaticallyAdjustsScrollViewInsets=NO有着同样的效果,不计算内边距
UIScrollViewContentInsetAdjustmentAlways//根据safeAreaInsets (安全区域)计算内边距
}
所以,在IOS11下为了防止页面或列表偏移,需要设置ScrollView:
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
如果需要全局设置的话,需要这么设置:
if (@available(iOS 11.0, *)) {
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
这样设置后使用UITableview 、UICollectionView、UIScrollview的时候就不需要再单独设置该属性了,因为UIView以及他的子类都是遵循UIAppearance协议的。
但是设置完成之后,调起系统通讯录或相册时,可能导致系统通讯录或相册上移、顶部出现空白。
有两种解决办法:
1.在进入通讯录或相册时,设置自动计算和适应顶部和底部的内边距。
if (@available(iOS 11.0, *)) {
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentAutomatic];
}
在退出通讯录或相册时再设置还原
if (@available(iOS 11.0, *)) {
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
2.设置相册的导航栏navigationBar为不透明,只对调起系统相册有效。原来导航栏的半透明效果去除,那么相册的布局坐标默认就从导航栏的下面开始。调起通讯录没有生效。
picker.navigationBar.translucent = NO;
网友评论