Swift:集合视图未正确调整以更改大小 [英] Swift: Collection View not adjusting correctly to change in size

查看:29
本文介绍了Swift:集合视图未正确调整以更改大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 6 个元素的集合视图,这些元素应该以 3 行和 2 列的形式排列.我使用以下代码来实现这一点:

I have a collection view with 6 elements that should be laid out with 3 rows and 2 columns. I use the following code to achieve this:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let totalWidth = collectionView.bounds.size.width - 12
    let totalHeight = collectionView.bounds.size.height - 18
    let heightOfView = (totalHeight / 3)
    let dimensions = CGFloat(Int(totalWidth))

        return CGSize(width: dimensions / 2, height: heightOfView)
}

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 6
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(0, 5, 0, 5)
}

在 viewDidLoad 中:

In viewDidLoad:

 override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.dataSource = self
    collectionView.delegate = self

    flowLayout.scrollDirection = .horizontal
    flowLayout.minimumLineSpacing = 5
    flowLayout.minimumInteritemSpacing = 5

这会产生一个看起来像这样的集合视图,这是完美的:

This produces a collection view that looks like this which is perfect:

这是所需的输出

但是,当我在加载视图之前根据情况隐藏集合视图下方的视图并调整其大小(使其高度高 50 pts)时,输出更改为以下(参见图像)单元格 4 &除非滚动到屏幕外 5 行,只有 2 行而不是 3 行,并且行之间有很大的差距:

However when I situationally hide a view below the collection view before the view is loaded and adjust the size of it(making it 50 pts taller in height), then the output changes to the following (see image) with cells 4 & 5 off the screen unless scrolled to, only 2 rows instead of 3 and a large gap between rows:

调整为隐藏视图时的集合视图外观

为了在 viewWillAppear 中隐藏视图,我添加了以下内容:

To situationally hide the view in viewWillAppear I added the following:

 override func viewWillAppear(_ animated: Bool) {

    if testVariable == true {
        bottomView.isHidden = true
        // make bottomView height = 0
        bottomViewHeight.constant = 0
        // make collectionView space to the bottom of the view controller 0
        collectionToBase.constant = 0
        view.layoutIfNeeded()
    } 
}

此代码测试 testVariable 是否为 true,如果为 true,则隐藏 bottomView 并使 collectionView 大 50 pts 以填充空间.我在 viewWillAppear 中添加了这段代码,希望 collectionViewLayout 会考虑额外的 50 pts 高度并根据需要进行调整,但它没有.

This code tests if testVariable is true, and if it is hides the bottomView and makes the collectionView 50 pts larger to fill the space. I added this code in viewWillAppear in the hope that collectionViewLayout would account for the extra 50 pts of height and adjust according, but it doesnt.

推荐答案

您需要使布局无效.它不会自己这样做.调用 layoutIfNeeded() 后,在流布局上调用 invalidateLayout().

You need to invalidate the layout. It does not do so on its own. Call invalidateLayout() on the flow layout after your call to layoutIfNeeded().

flowLayout.invalidateLayout()

这篇关于Swift:集合视图未正确调整以更改大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆