[英]Swift: Let a UICollectionView dynamically change its size
Target: I have a UICollectionView with default 7 columns and 6 rows of cells. 目标:我有一个UICollectionView,默认7列和6行单元格。 Now the user shall be able to change the amount of columns and rows via a UIStepper.
现在,用户可以通过UIStepper更改列数和行数。 Like so:
像这样:
var numberOfColumns: Int = 7
@IBAction func changeNumberOfColumns(_ sender: UIStepper) {
numberOfColumns = Int(sender.value)
boardSizingCollectionView.reloadData()
}
extension BoardSizingViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return numberOfRows
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfColumns
}
func collectionView(_ collectionsiView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Field", for: indexPath) as! FieldCollectionViewCell
cell.tokenView.backgroundColor = .white
cell.tokenView.layer.cornerRadius = (cell.frame.width - 8 * 2) / 2
return cell
}
}
The cells shall keep their size, instead the UICollectionView shall be resized. 单元格应保持其大小,而不应调整UICollectionView的大小。
Problem: I can't directly set the width and height of a UICollectionView in code, because these are get-only properties. 问题:我无法在代码中直接设置UICollectionView的宽度和高度,因为这些是只获取属性。 Also if I use
如果我使用的话
boardSizingCollectionView.frame.size = CGSize(width: FieldCollectionViewCell.frame.width * numberOfColumns, height: FieldCollectionViewCell.frame.height * numberOfRows)
I get an error, because my FieldCollectionViewCell is a CGRect and i don't know how to exactly access the size of a CGRect. 我收到一个错误,因为我的FieldCollectionViewCell是一个CGRect,我不知道如何准确访问CGRect的大小。
Another hook in the flesh is, that myCollectionView has constraints for it's x and y position, to be auto-positioned on different devices so myCollectionView has to get a width and a height at least in viewDidLoad(). 肉体中的另一个钩子是,myCollectionView对其x和y位置有约束,要自动定位在不同的设备上,因此myCollectionView必须至少在viewDidLoad()中获得宽度和高度。
Any hints for me? 有什么提示吗? Thank you very much in forward!
非常感谢你!
You can not modify the frame
directly. 您无法直接修改
frame
。 But, You can assign a new frame for the question. 但是,您可以为问题分配新框架。
I can't directly set the width and height of a UICollectionView in code, because these are get-only properties.
我无法在代码中直接设置UICollectionView的宽度和高度,因为这些是只获取属性。 Also if I use
如果我使用的话
//get the current frame
var collectionViewFrame = self.collectionView.frame
//set the size
collectionViewFrame.size = CGSize(width: FieldCollectionViewCell.frame.width * numberOfColumns, height: FieldCollectionViewCell.frame.height * numberOfRows)
//again set the frame
self.collectionView.frame = collectionViewFrame
Maybe try a different approach in constraining your collection view in storyboard, such that you are able to change the height through constraints. 也许尝试一种不同的方法来约束故事板中的集合视图,这样您就可以通过约束来改变高度。 One way would be to give it a height constraint, and connect an outlet to your view controller.
一种方法是给它一个高度约束,并将插座连接到视图控制器。 This way you can do
这样你就可以做到
collectionViewHeightConstraint.constant =
FieldCollectionViewCell.frame.height * numberOfRows
collectionView.layoutIfNeeded()
collectionView.reloadData()
Similarly, giving the collection view a top and bottom constraint, then changing the bottom constraint also works, if you can't give it a height constraint. 类似地,给集合视图一个顶部和底部约束,如果你不能给它一个高度约束,那么改变底部约束也是有效的。
I haven't try the code yet let me know if it works :) 我没有尝试代码,但让我知道它是否有效:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.