問題描述:
在部分使用collectionView封裝ViewController的頁面時锹雏,如果需要presentviewcontroller
到部分橫屏頁面進行相關操作時亡笑,會導致collectionView滑動而對未初始化的ViewController進行ViewDidLoad操作导饲,這時候如果定義了宏獲取View.bounds時會發(fā)現獲取到的是橫屏的bounds,但是ViewController使用的是豎屏渠欺,這時候BUG出現虱颗。要解決presentviewcontroller
時不讓collectionView進行無關的滑動,這時候需要在為collectionView的contentInsetAdjustmentBehavior
屬性賦值為UIScrollViewContentInsetAdjustmentNever
即可解決聪黎。如下:
collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever
擴展:
UIScrollView的contentInsetAdjustmentBehavior屬性包含以下四種:
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller, regardless of whether the scroll view is scrollable
UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));
UIScrollViewContentInsetAdjustmentAutomatic
類似于UIScrollViewContentInsetAdjustmentScrollableAxes罕容,scrollView會自動計算和適應頂部和底部的內邊距,并且在scrollView不可滾動時稿饰,也會設置內邊距
UIScrollViewContentInsetAdjustmentScrollableAxes
自動計算內邊距
.UIScrollViewContentInsetAdjustmentNever
不計算內邊距
UIScrollViewContentInsetAdjustmentAlways
根據safeAreaInsets計算內邊距