iOS 11&iPhone X上のUIscrollView(UItableView)コンテンツオフセットの問題
4916 ワード
の原因となる iOS 11の 補足:ナビゲーションバーがある場合
安全区域セキュリティエリアはiOS 11が新たに提案したものです. セキュリティ領域は、
adjustContentInsetプロパティの計算方法
まず、iOS 11に追加された である.
解決策は、 iOS 11
グループヘッダグループテール高さ iOS 11以降は、プロキシメソッド
UIViewController
のautomaticallyAdjustsScrollViewInsets
属性はもう使用されていません.UIScrollView
のcontentInsetAdjustmentBehavior
属性を使用して代替する必要があります.UIScrollView
のframe
が安全領域の範囲を超えた場合、システムはUIScrollView
のsafeAreaInsets
値を自動的に調整し、UIScrollView
のadjustContentInset
値に影響を与え、UIScrollView
の内容がずれる.iOS 11がUIScrollView
のコンテンツオフセット量を制御する属性はadjustContentInset
であり、adjustContentInset
の値はシステムがsafeAreaInsets
、contentInset
に基づいて計算したものであり、計算方式はcontentInsetAdjustmentBehavior
によって決定される.safeAreaInsets
値は自動的に(88,0,34,0)に調整されます.ナビゲーションバーがない場合のsafeAreaInsets
の値は(44,0,34,0)です.安全区域
view
を画面全体の可視部分に配置するのに役立ちます.navigationBar
を透明に設定しても、システムは、navigationBar
のbottom
から安全領域が始まり、システムのステータスバーやナビゲーションバーに覆われないことを保証していると考えている.controller
は、additionalSafeAreaInsets
を使用して、インタフェース上にカスタムcontentを含むようにセキュリティ領域を拡張することができる.各view
は、セキュリティ領域埋め込みの大きさを変更することができ、controller
も可能である.safeAreaInsets
の属性は、view
からview
までの安全領域の余白を反映する.UIViewController
のルートビューの場合、safeAreaInsets
の値は、statusbar
および他の視覚的bars
によって覆われた領域、およびadditionalSafeAreaInsets
によってカスタマイズされた他のinsets
の値を含む.view
階層の他のview
safeAreaInsets
値は、view
が上書きされた部分を反映している.view
がすべて親ビューのセキュリティ領域内にある場合、safeAreaInsets
の値は(0,0,0,0)です.adjustContentInsetプロパティの計算方法
まず、iOS 11に追加された
UIScrollView
の2つのプロパティ:adjustContentInset
とcontentInsetAdjustmentBehavior
を参照してください.UIScrollViewContentInsetAdjustmentAutomatic
:scrollView
がautomaticallyAdjustsScrollViewContentInset = YES
のUIViewController
にあり、このUIViewController
がUINavigationController
に含まれている場合、スクロールの有無にかかわらずtop & bottom
に設定されます.その他の場合はadjustedContentInset = safeAreaInset + contentInset
と同じです.UIScrollViewContentInsetAdjustmentScrollableAxes
:スクロール可能な方向にUIScrollViewContentInsetAdjustmentScrollableAxes
、スクロール不可能な方向にadjustedContentInset = safeAreaInset + contentInset
;adjustedContentInset = contentInset
およびscrollEnabled
alwaysBounceHorizontal / Vertical = YES
に依存するデフォルトはscrollEnabled
であるため、多くの場合、計算方法はYes
adjustedContentInset = safeAreaInset + contentInset
: UIScrollViewContentInsetAdjustmentNever
.adjustedContentInset = contentInset
がcontentInsetAdjustmentBehavior
に設定されている場合、UIScrollViewContentInsetAdjustmentNever
の値はadjustContentInset
の値の影響を受けない.safeAreaInset
: UIScrollViewContentInsetAdjustmentAlways
解決策
adjustedContentInset = safeAreaInset + contentInset
のtableView
値を再設定し、contentInset
値を相殺する.safeAreaInset
に設定されたadjustedContentInset = contentInset + safeAreaInset
の属性はtableView
であり、contentInsetAdjustmentBehavior
である. // API:`@available(iOS 11.0, *)`
if ( @available(iOS 11.0, *) ) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
UIScrollViewContentInsetAdjustmentNever
新規属性adjustedContentInset = contentInset
を設定します.iOS 11以前は、UIViewController
のaddtionalSafeAreaInset
属性をNOに設定することで、UIViewController
のautomaticallyAdjustsScrollViewInsets
をシステム調整することを禁止していました.tableView
のレベルから問題を解決するには、contentInset
のcontroller
の属性を設定することによって、controller
のadditionalSafeAreaInsets
がセキュリティ領域の範囲を超えていない場合、システムはtableView
のframe
の値を調整せず、コンテンツオフセットも発生しないため、問題を解決することができる.グループヘッダグループテール高さ
tableView
のタイプがsafeAreaInset
である場合、システムのデフォルトのtableView
のヘッダとヘッダはピッチがあり、このピッチが必要でない場合、iOS 11は、エージェントメソッドUITableViewStyleGrouped
を実装することによって(より小さな値:0.01を返す)前に解決することができる.tableView
だけでなく、プロキシメソッドheightForHeaderInSection / heightForFooterInSection
を実装してこそ、間隔を取り除くことができる.あるいは以下のコードを追加して高さ推定を閉じても問題は解決できます.self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;