UICollectionView高度自動(dòng)調(diào)整
頁(yè)面滑動(dòng)的過(guò)程中,banner高度會(huì)自動(dòng)發(fā)生改變,得到提示如下信息:
The behavior of the
UICollectionViewFlowLayoutis 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
UICollectionViewFlowLayoutinstance is <UICollectionViewFlowLayout: 0x7fde80753e60>, and it is attached to <UICollectionView: 0x7fde608b6e00; baseClass = UICollectionView; frame = (0 44; 345 94); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60000055c270>; layer = <CALayer: 0x60000080f1e0>; contentOffset: {692, 0}; contentSize: {7590, 138}; adjustedContentInset: {0, 0, 0, 0}; layout: <UICollectionViewFlowLayout: 0x7fde80753e60>; dataSource: <BannerViewController: 0x7fde81198c50>>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
簡(jiǎn)單來(lái)說(shuō)就是:UICollectionViewFlowLayout的itemSize的寬或者高設(shè)置的有問(wèn)題!UICollectionView的size必須在父容器的范圍之內(nèi)!
我們可以從多個(gè)方面來(lái)確認(rèn)
1、確認(rèn)UICollectionViewFlowLayout的itemSize是否正確
itemSize有2種方式設(shè)置
- 實(shí)現(xiàn)協(xié)議返回大小
extension BannerViewController: UICollectionViewDelegateFlowLayout {
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
return CGSize(width: Constants.itemWidth, height: Constants.itemHeight)
}
}
- 設(shè)置UICollectionView的flowLayout
var flowLayout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.sectionInset = .zero
layout.itemSize = CGSize(width: Constants.itemWidth, height: Constants.itemHeight)
return layout
}()
2、確認(rèn)sectionInset的大小

func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets.zero
}
也可以如上flowLayout設(shè)置sectionInset大小
3、確認(rèn)UICollectionView和容器視圖的大小
假設(shè)你的UICollectionView的高度是100,這時(shí)你的UICollectionViewFlowLayout的itemSize的高度也設(shè)置的是100,那么你的UIEdgeInsetsMake的頂部和底部的距離就應(yīng)該是0,如果你還強(qiáng)行設(shè)置一個(gè)大于0的高度,就會(huì)報(bào)上面警告。換成寬度也是一樣。所以UICollectionView的寬高要設(shè)置合適才行
4、確認(rèn)contentInset是否正確
因?yàn)榛瑒?dòng)的時(shí)候,可能會(huì)自動(dòng)調(diào)整contentInset,這時(shí)候需要設(shè)置
collectionView.contentInsetAdjustmentBehavior = .never // never 不調(diào)整cententinset
@available(iOS 11.0, *)
public enum ContentInsetAdjustmentBehavior : Int {
case automatic = 0 // 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 = 1 // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
case never = 2 // contentInset is not adjusted
case always = 3 // contentInset is always adjusted by the scroll view's safeAreaInsets
}
5、確認(rèn)約束布局是否正確
首先確認(rèn)UICollectionView的布局是否正確,然后方便確認(rèn)子視圖約束布局,還可以確認(rèn)下面SafeArea:
因?yàn)榛瑒?dòng)的時(shí)候,可能會(huì)自動(dòng)調(diào)整safeAreaInsets,如果你的布局依賴于SafeArea,那么就需要注意了,因?yàn)?code>UICollectionView高度固定大小,而SafeArea的頂部或者底部如果存在值,那么整體展示高度必然減小,可以嘗試打印safeAreaInsets數(shù)據(jù)看看
如:Storyboard中添加相對(duì)SafeArea的布局

這時(shí)候就需要修改為相對(duì)SuperView布局
