首页 > 解决方案 > 如何在按钮单击时更改 UICollectionView 高度

问题描述

当我单击按钮时,我正在尝试更改集合视图和视图的高度。但是当我单击按钮时,视图的高度会发生变化,但集合视图的高度不会发生变化。

这是我的代码:

@IBAction func button(_ sender: Any) {
        UIView.animate(withDuration: 1, animations: {
            self.backView.frame.size.height -= 160
            self.collectionView.frame.size.height -= 160
            UIView.animate(withDuration: 0.3, delay: 0, options:     .curveEaseOut,
                                       animations:     self.view.layoutIfNeeded, completion: nil)

        }, completion: nil)
    }

我还能做些什么来调整集合视图的大小?

标签: iosswiftxcodeuicollectionview

解决方案


如果您在该集合视图上使用了约束,则需要更改高度约束的常量,而不是其框架。像这样:

@IBAction func button(_ sender: Any) {
        UIView.animate(withDuration: 1, animations: {
              self.backView.frame.size.height -= 160
              self.collectionViewHeightConstraint.constant = <value>
              self.view.layoutIfNeeded()
            }, completion: nil)
        }

推荐阅读