美文网首页
UICollectionView滑动高度自动调整,提示布局问题:

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

作者: Longshihua | 来源:发表于2021-12-25 10:11 被阅读0次

    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)
        }
    }
    
    • 设置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、确认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

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/yacdqrtx.html