0

所以我在 UIView 中有一个集合视图,它包含在滚动视图中。(见下文) 在此处输入图像描述

集合视图中的内容会根据所选选项卡更改大小。当集合视图中的项目数量很少(IE <= 4 项目)时,我想在屏幕底部显示下一个按钮。如果集合视图的内容较大(IE > 4 项),我希望允许向下滚动屏幕(当前有效),并将下一个按钮放置在集合视图的内容下方。

在第一次加载屏幕时,在具有 <=4 项的选项卡上,下一个按钮被放置在正确的位置。如果我切换到超过 4 个玩家的选项卡,下一个按钮会按预期正确放置在集合视图下方。但是,当我切换回 <=4 玩家的选项卡时,下一个按钮被放置在错误的位置,在屏幕上更高。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

现在回到玩家较少的选项卡,问题出现了:

在此处输入图像描述

在此处查看问题:

https://drive.google.com/file/d/12eREBZlBZoCs-F5DJd-I26YcLc3pRqRJ/view?usp=sharing

请注意,当我从前锋切换回中场时,下一个按钮的位置不正确,而第一次在中场上,下一个按钮的位置是正确的。

我用于调整集合视图大小和更改下一个按钮位置的代码如下。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
        //Setup Img & Label
        cell.playerImageView.image = playerArray!.filter({ $0.position == pos })[indexPath.row].image

        resizeCollectionView() //Setup view size after items calculated
        return cell
    }

调整大小方法(上面调用):

func resizeCollectionView() {

        self.collectionViewHeightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
        if self.playerArray!.filter({ $0.position == pos }).count <= 4 {
            self.nextBtn.topAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -120).isActive = true
        }
        else {
            self.nextBtn.topAnchor.constraint(equalTo: collectionView.bottomAnchor, constant: 50).isActive = true            
        }
        self.nextBtn.translatesAutoresizingMaskIntoConstraints = false
        self.view.layoutIfNeeded()
        }

用 Swift 编写 4

谁能指出我为什么会出现这个问题的正确方向?提前致谢!

编辑:@Dris,当我为约束添加更改时,结果是: 在此处输入图像描述

4

0 回答 0