最近用UICollectionView寫了一個卡片輪播,ios12上效果完美,在ios10上跑了一下發現視圖出現偏移,效果如下圖
找了好久原因最后發現在ios10上控制台打印了如下提示內容
The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. The relevant UICollectionViewFlowLayout instance is <CollectionFlowLayout: 0x7f83d7d5d230>, and it is attached to <UICollectionView: 0x7f83d8111000; frame = (0 0; 375 160); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000058ba0>; layer = <CALayer: 0x600000221460>; contentOffset: {0, 0}; contentSize: {126, 111}> collection view layout: <CollectionFlowLayout: 0x7f83d7d5d230>. Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
上網查了下原因發現在ios11以下系統中導航控制器下當self.automaticallyAdjustsScrollViewInsets = YES;時,控制器下的scrollview的contentInset會默認向下偏移64,所以把這個屬性設為NO就可以了,希望對遇到同樣問題的朋友有幫助