iOS 适配问题

本文详细介绍了在iOS 11中遇到的适配问题,包括导航栏高度变化,titleView显示异常,tableView内容偏移,以及UITableViewStyleGrouped类型的顶部留白问题。提供了相应的解决方案,如重写intrinsicContentSize,调整tableView的adjustedContentInset和contentInsetAdjustmentBehavior属性,以及关闭估算行高等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.导航栏高度的变化

iOS11之前导航栏默认高度为64pt(这里高度指statusBar + NavigationBar),iOS11之后如果设置了prefersLargeTitles = YES则为96pt,默认情况下还是64pt,但在iPhoneX上由于刘海的出现statusBar由以前的20pt变成了44pt,所以iPhoneX上高度变为88pt,如果项目里隐藏了导航栏加了自定义按钮之类的,这里需要注意适配一下。

 

2. 在iOS 11中titleView的宽度设置了没有效果,也没有拉伸展示,缩成一坨了。项目中有好多地方用到了titleView这个属性,怎么办?

打开Xcode 查看视图层级功能,发现在iOS 11 中Apple 改变了UINavigationBar的视图层级,titleView不是加到NavigationBar上了,而是加到UINavigationBarContentView上

那么怎么解决这个问题呢?

在你自定义的titleView中重写intrinsicContentSize 的Get 方法,如下:

 

- (CGSize)intrinsicContentSize

{

    return UILayoutFittingExpandedSize;

}

然后再次运行项目,你会发现和之前的效果一样了。

 

3. 在iOS11设备上运行出现tableview莫名奇妙的往上或则往下偏移20pt或者64pt了。

原因是iOS 11中Controller的automaticallyAdjustsScrollViewInsets属性被废弃了,当tableView超出安全区域时系统自动调整了tableView的safeAreaInsets值,进而影响adjustedContentInset值.而在在iOS 11中决定tableView的内容与边缘距离的是adjustedContentInset属性,而不是contentInset。scrollView在iOS11新增的两个属性:adjustContentInset 和 contentInsetAdjustmentBehavior。adjustContentInset表示contentView.frame.origin偏移了scrollview.frame.origin多少;是系统计算得来的,计算方式由contentInsetAdjustmentBehavior决定。有以下几种计算方式:

1. UIScrollViewContentInsetAdjustmentAutomatic:如果scrollview在一个automaticallyAdjustsScrollViewInsets = YES的controller上,并且这个Controller包含在一个navigation controller中,这种情况下会设置在top & bottom上 adjustedContentInset = safeAreaInset + contentInset不管是否滚动。其他情况下与UIScrollViewContentInsetAdjustmentScrollableAxes相同.

2. UIScrollViewContentInsetAdjustmentScrollableAxes: 在可滚动方向上adjustedContentInset = safeAreaInset + contentInset,在不可滚动方向上adjustedContentInset = contentInset;依赖于scrollEnabled和alwaysBounceHorizontal / vertical = YES.

3. UIScrollViewContentInsetAdjustmentNever: adjustedContentInset = contentInset

4. UIScrollViewContentInsetAdjustmentAlways: adjustedContentInset = safeAreaInset + contentInset

 

当contentInsetAdjustmentBehavior设置为UIScrollViewContentInsetAdjustmentNever的时候,adjustContentInset值不受SafeAreaInset值的影响。

4. iOS 11上tableView的style:UITableViewStyleGrouped类型时顶部有留白。

原因是代码中只实现了heightForHeaderInSection(返回一个较小值:0.1)方法,而没有实现viewForHeaderInSection方法。那样写是不规范的,只实现高度,而没有实现view,但代码这样写在iOS 11之前是没有问题的,iOS 11之后是由于开启了估算行高机制引起了bug。添加上viewForHeaderInSection方法后,问题就解决了。或者添加以下代码关闭估算行高,问题也得到解决。

tableView.estimatedRowHeight = 0;

tableView.estimatedSectionHeaderHeight = 0;

tableView.estimatedSectionFooterHeight = 0;

 

iOS11UI新特性介绍

Updating Your App for iOS 11 - WWDC 2017 - Session 204 - iOS

http://www.jianshu.com/p/370d82ba3939

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值