UICollectionView滑动高度自动调整,提示布局问题:

2021-12-25  本文已影响0人  Longshihua

UICollectionView高度自动调整

页面滑动的过程中,banner高度会自动发生改变,得到提示如下信息:

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 <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.

简单来说就是:UICollectionViewFlowLayoutitemSize的宽或者高设置的有问题!UICollectionViewsize必须在父容器的范围之内!

我们可以从多个方面来确认

1、确认UICollectionViewFlowLayout的itemSize是否正确

itemSize有2种方式设置

extension BannerViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(
        _ collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        sizeForItemAt indexPath: IndexPath
    ) -> CGSize {
       return CGSize(width: Constants.itemWidth, height: Constants.itemHeight)
    }
}
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、确认sectionInset的大小

collectionview3.png
func collectionView(_ collectionView: UICollectionView, layout
                    collectionViewLayout: UICollectionViewLayout,
                    insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets.zero
}

也可以如上flowLayout设置sectionInset大小

3、确认UICollectionView和容器视图的大小

假设你的UICollectionView的高度是100,这时你的UICollectionViewFlowLayout的itemSize的高度也设置的是100,那么你的UIEdgeInsetsMake的顶部和底部的距离就应该是0,如果你还强行设置一个大于0的高度,就会报上面警告。换成宽度也是一样。所以UICollectionView的宽高要设置合适才行

4、确认contentInset是否正确

因为滑动的时候,可能会自动调整contentInset,这时候需要设置

collectionView.contentInsetAdjustmentBehavior = .never // never 不调整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、确认约束布局是否正确

首先确认UICollectionView的布局是否正确,然后方便确认子视图约束布局,还可以确认下面SafeArea

因为滑动的时候,可能会自动调整safeAreaInsets,如果你的布局依赖于SafeArea,那么就需要注意了,因为UICollectionView高度固定大小,而SafeArea的顶部或者底部如果存在值,那么整体展示高度必然减小,可以尝试打印safeAreaInsets数据看看

如:Storyboard中添加相对SafeArea的布局

截屏2021-12-25 上午9.48.05.png

这时候就需要修改为相对SuperView布局

截屏2021-12-25 上午9.51.06.png
上一篇下一篇

猜你喜欢

热点阅读