iOS11 和 iPhone X 适配

2017-11-08  本文已影响126人  Auther丶

一般的适配

1.

if(@available(iOS11.0, *)){

_tableView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;

_tableView.contentInset=UIEdgeInsetsZero;

}

2.自定义导航条高度由20+44 = 64变为 44+44 = 88( 或者 44+34 = 78 自己项目适用)

3.注意 横/竖 屏时 safeArea 边界 按钮      如果是竖屏 list信息流形式的 苹果允许全屏展示

4.tableView 滑动刷新跳动 注意将 headerfooter 的 viewForHeader/Footer heightForHeader / Footer 都实现   加 estimated row/header/footer 三个值写上。 或者collectionView中 layout的默认值

5.如果点了 statusBar 后 tableView不能滚动到顶部 就要写 self.tableView..scrollsToTop=YES;

6.注意collectionView header 上添加 子空间 图层错乱问题 可以converPoint之后加在 collectionView.superView上面

参考文献 

iOS 11 SafeArea

iOS 7之后有TopLayoutGuide

由currentViewController屏幕上方任意遮挡内容的栏的最底部 (即可见的 statusBar 和 可见的navigationBar )

iOS 11 之后 废弃 Top BottomLayoutGuide

引入UIViewSafeArea (任何可点击的空间不要放在 SafeArea 之外的地方)

UIView 的 SafeArea

UIEdgeInsets safeAreaInsets在 ViewDidAppear或者 didLayoutSubViews 中可以打印当前的 safeAreaInsets而且只有在某个View处在 safeArea之外时 才会有大于0 的值

解决办法一在布局父控件时 使用 window的safeAreaInsets控制 四个边距

let insets = UIApplication.shared.delegate?.window??.safeAreaInsets ?? UIEdgeInsets.zero

view1.frame = CGRect(

x: insets.left,

y: insets.top,

width:view.bounds.width - insets.left - insets.right,

height: 200)

view2.frame = CGRect(

x: insets.left,

y: screenH - insets.bottom - 200,

width:view.bounds.width - insets.left - insets.right,

height: 200)

解决办法 二在父控件中 布局子空间时使用自己的 safeAreaLayoutGuide.layoutFrame

注意 以上是 UIView的 SafeArea

UIViewController 中的 SafeArea

@available(iOS 11.0, *)

open var additionalSafeAreaInsets: UIEdgeInsets

View 和 VC对应的KVO change回调方法

// UIView

@available(iOS 11.0, *)

open func safeAreaInsetsDidChange()

//UIViewController

@available(iOS 11.0, *)

open func viewSafeAreaInsetsDidChange()

滚动视图相关

废弃 iOS 7 VC中的automaticallyAdjustsScrollViewInsets替代的是 iOS 11 ScrollView中的 contentInsetAdjustmentBehavior

@available(iOS 11.0, *)

public enum UIScrollViewContentInsetAdjustmentBehavior : Int {

case automatic//default value

case scrollableAxes//contentInsets 只会针对scrollView滚动方向做调整

case never// 不做调整

case always// 上下 左右两个滚动方向均做调整

}

automatic 分两种情况 当是以下这四种情景之一时 等同于 always

1.能够水平滚动,不能垂直滚动

2.scroll view 是 当前 view controller 的第一个视图

3.这个controller 是被navigation controller 或者 tab bar controller 管理的

4.automaticallyAdjustsScrollViewInsets 为 true

在其他情况下 automoatc 跟 scrollableAxes 一样

@available(iOS 11.0, *)

open var contentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentBehavior

Adjusted Content Insets

iOS 11 中 UIScrollView 新加了一个属性: adjustedContentInset

@available(iOS 11.0, *)

open var adjustedContentInset: UIEdgeInsets { get }

adjustedContentInset 和 contentInset 之间有什么区别呢?

在同时有 navigation 和 tab bar 的 view controller 中添加一个 scrollview 然后分别打印两个值:

//iOS 10

//contentInset = UIEdgeInsets(top: 64.0, left: 0.0, bottom: 49.0, right: 0.0)

//iOS 11

//contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

//adjustedContentInset = UIEdgeInsets(top: 64.0, left: 0.0, bottom: 49.0, right: 0.0)

然后再设置:

// 给 scroll view 的四个方向都加 10 的间距

scrollView.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

打印:

//iOS 10

//contentInset = UIEdgeInsets(top: 74.0, left: 10.0, bottom: 59.0, right: 10.0)

//iOS 11

//contentInset = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)

//adjustedContentInset = UIEdgeInsets(top: 74.0, left: 10.0, bottom: 59.0, right: 10.0)

由此可见,在 iOS 11 中 scroll view 实际的 content inset 可以通过 adjustedContentInset 获取。这就是说如果你要适配 iOS 10 的话。这一部分的逻辑是不一样的。

系统还提供了两个方法来监听这个属性的改变

//UIScrollView

@available(iOS 11.0, *)

open func adjustedContentInsetDidChange()

//UIScrollViewDelegate

@available(iOS 11.0, *)

optional public func scrollViewDidChangeAdjustedContentInset(_ scrollView: UIScrollView)

UITableView 中的 safe area

@available(iOS 11.0, *)

open var insetsContentViewsToSafeArea: Bool

insetsContentViewsToSafeArea 的默认值是 true

UICollectionView 中的 safe area

左右两边的 cell 都被刘海挡住了。这种情况下, 我们可以通过修改 section insets 来适配 safe area 来解决这个问题。但是再 iOS 11 中, UICollectionViewFlowLayout 提供了一个新的属性 sectionInsetReference 来帮你做这件事情。

@available(iOS 11.0, *)

public enum UICollectionViewFlowLayoutSectionInsetReference : Int {

case fromContentInset

case fromSafeArea

case fromLayoutMargins

}

@available(iOS 11.0, *)

open var sectionInsetReference: UICollectionViewFlowLayoutSectionInsetReference

系统默认是使用 .fromContentInset

上一篇 下一篇

猜你喜欢

热点阅读