今天遇到用UICollectionView使用轮播图遇到的问题:
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.
打印如下:
2018-03-19 17:05:36.501659+0800 CheckBox[18573:492773] The behavior of the UICollectionViewFlowLayout is not defined because:
2018-03-19 17:05:36.501749+0800 CheckBox[18573:492773] 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.
2018-03-19 17:05:36.501940+0800 CheckBox[18573:492773] The relevant UICollectionViewFlowLayout instance is <CheckBox.WMRunScrollViewFlowLayout: 0x7f9fb83025e0>, and it is attached to <UICollectionView: 0x7f9fb686ca00; frame = (0 0; 375 200); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x6080002479e0>; layer = <CALayer: 0x608000428b80>; contentOffset: {112500, -24}; contentSize: {0, 0}; adjustedContentInset: {24, 0, 0, 0}> collection view layout: <CheckBox.WMRunScrollViewFlowLayout: 0x7f9fb83025e0>.
2018-03-19 17:05:36.502026+0800 CheckBox[18573:492773] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
解决办法:
override func viewDidLoad() {
super.viewDidLoad()
//自动计算滚动视图的内容边距
self.automaticallyAdjustsScrollViewInsets = false //不自动计算
}
iOS11之后采用:
self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.never;
关于UIScrollViewContentInsetAdjustmentBehavior的枚举类型
@available(iOS 11.0, *)
public enum UIScrollViewContentInsetAdjustmentBehavior : Int {
case automatic // 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
case scrollableAxes // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
case never // contentInset is not adjusted
case always // contentInset is always adjusted by the scroll view's safeAreaInsets
}
- automatic 与.scrollableAxes类似,但为了向后兼容,当滚动视图由视图控制器拥有时,也会调整顶部和底部contentInset,并且在导航控制器中自动调用ScrollViewInsets = YES,而不管滚动视图是否可滚动
- scrollableAxes 调整可滚动轴的边缘(即,contentSize.width / height> frame.size.width / height或alwaysBounceHorizontal / Vertical = YES)
- never 不调整cententinset
- always 总是通过滚动视图的safeAreaInsets进行调整contentInset