iOS 滾動視圖內容自動偏移總結


[iOS7 , iOS11):
viewcontroller參數automaticallyAdjustsScrollViewInsets決定其中scrollview偏移量參數contentInset:

默認情況下:automaticallyAdjustsScrollViewInsets = YES,contentInset = {64,0,0,0},tabbarcontroller情況下為{64,0,49,0},很容易理解假設設置scorllview的frame為屏幕大小,其可控高度為其高度減掉導航條的64和tabbar的49

導航條隱藏時,兩種情況下contentInset分別是{20,0,0,0},{20,0,49,0},20是狀態欄的高度

 

[iOS11,~):

viewcontroller新增所謂安全區域,帶來一系列參數:SafeAreaInsets、additionalSafeAreaInsets、adjustedContentInset、contentInsetAdjustmentBehavior……;

原來的automaticallyAdjustsScrollViewInsets失效了,新的方案認為狀態欄導航條、tabbar之間的區域為安全區域

scrollview的最終偏移量adjustedContentInset = safeAreaInset + contentInset,safeAreaInset的值與iOS 11之前的contentInset相同,iOS11中contentInset默認為{0,0,0,0}

導航條隱藏時,兩種情況下safeAreaInset分別是{20,0,0,0},{20,0,49,0},20是狀態欄的高度

 

contentInsetAdjustmentBehavior參數:

  UIScrollViewContentInsetAdjustmentAutomatic:如果scrollview在一個controller上,並且這個Controller包含在一個navigation controller中,這種情況下會設置在top & bottom上 adjustedContentInset = safeAreaInset + contentInset不管是否滾動。其他情況下與UIScrollViewContentInsetAdjustmentScrollableAxes相同

  UIScrollViewContentInsetAdjustmentScrollableAxes: 在可滾動方向上adjustedContentInset = safeAreaInset + contentInset,在不可滾動方向上adjustedContentInset = contentInset;依賴於scrollEnabled和alwaysBounceHorizontal / vertical = YES,scrollEnabled默認為yes,所以大多數情況下,計算方式還是adjustedContentInset = safeAreaInset + contentInset

  UIScrollViewContentInsetAdjustmentNever: adjustedContentInset = contentInset

  UIScrollViewContentInsetAdjustmentAlways: adjustedContentInset = safeAreaInset + contentInset

當contentInsetAdjustmentBehavior設置為UIScrollViewContentInsetAdjustmentNever的時候,adjustContentInset值不受SafeAreaInset值的影響。

如果你想抵消這種偏移,添加additionalSafeAreaInsets參數。
 

如果你被蘋果這個設計惡心到了

if (@available(iOS 11.0, *)) {
        self.table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

 

另外注意:edgesForExtendedLayout和extendedLayoutIncludesOpaqueBars

這兩個參數導致的view偏移問題

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM