简体   繁体   English

Swift:集合视图未正确调整以更改大小

[英]Swift: Collection View not adjusting correctly to change in size

I have a collection view with 6 elements that should be laid out with 3 rows and 2 columns.我有一个包含 6 个元素的集合视图,这些元素应该用 3 行和 2 列进行布局。 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)
}

In viewDidLoad:在 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:这会产生一个看起来像这样的集合视图,这是完美的:

This is the desired output这是所需的输出

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

Collection view appearance when adjusted to account for hidden view调整以考虑隐藏视图时的集合视图外观

To situationally hide the view in viewWillAppear I added the following:为了在 viewWillAppear 中隐藏视图,我添加了以下内容:

 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()
    } 
}

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.此代码测试 testVariable 是否为真,如果为真,则隐藏底部视图并使 collectionView 大 50 pts 以填充空间。 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.我在 viewWillAppear 中添加了这段代码,希望 collectionViewLayout 可以考虑额外的 50 pts 高度并根据需要进行调整,但事实并非如此。

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().在调用 layoutIfNeeded() 之后,在流布局上调用 invalidateLayout()。

flowLayout.invalidateLayout()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM