ios Swift,以编程方式更改 UICollectionViewCell 和 UILabel(单元格内)的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27677956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:09:04  来源:igfitidea点击:

Swift, Change width of UICollectionViewCell and UILabel(inside the cell) programmatically

iosswiftuicollectionviewuicollectionviewcelluicollectionviewlayout

提问by manosim

I've set the width of a cell(UICollectionViewCell) to be equal to the width of the UICollectionView and I'm trying to do exactly the same thing with the UILabel that is included inside that cell. I think the code below explains exactly what I'm trying to achieve. So i've read some question here in SO and also a couple of tutorials but I'm still not sure how I can achieve this.

我已将单元格 (UICollectionViewCell) 的宽度设置为等于 UICollectionView 的宽度,并且我正在尝试对该单元格内包含的 UILabel 执行完全相同的操作。我认为下面的代码准确地解释了我想要实现的目标。所以我在这里阅读了一些问题以及一些教程,但我仍然不确定如何实现这一点。

In a couple of questions it was saying about using collectionViewLayoutbut I'm really struggling on how to use it within my code. Any ideas? Thank you!

在几个问题中,它说的是使用,collectionViewLayout但我真的很想知道如何在我的代码中使用它。有任何想法吗?谢谢!

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as LocationViewCell
    cell.locationLabel.text = "Hello there!"

    // Set cell & label width to 100%
    let collectionViewWidth = self.collectionView.bounds.size.width
    cell.frame.size.width = collectionViewWidth // Works
    cell.locationLabel.frame.size.width = collectionViewWidth // Does NOT 

Update 1

更新 1

So I added the following:

所以我添加了以下内容:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    // Set cell width to 100%
    let collectionViewWidth = self.collectionView.bounds.size.width
    return CGSize(width: collectionViewWidth, height: 35)
}

What happens is that when the view is loaded the UILabel's width is still small. If I go to another view and then return back then it's 100%. So I have to do something in the viewDidLoad()right? I'm already using self.collectionView.reloadData()but I guess that's only for data.

发生的情况是当视图加载时 UILabel 的宽度仍然很小。如果我转到另一个视图然后返回,则为 100%。所以我必须做一些viewDidLoad()正确的事情?我已经在使用,self.collectionView.reloadData()但我想这仅适用于数据。

Update 2

更新 2

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("locationCell", forIndexPath: indexPath) as LocationViewCell
    cell.locationLabel.text = "Hello UILabel"

    // Set cell width to 100%
    let collectionViewWidth = self.collectionView.bounds.size.width
    cell.frame.size.width = collectionViewWidth
    cell.locationLabel.frame.size.width = collectionViewWidth

    return cell
}

回答by Abizern

It doesn't work because by the time this method is called, the collection view already knows how big the cell should be because it has got it from the flow delegate method:

它不起作用,因为在调用此方法时,集合视图已经知道单元格应该有多大,因为它是从流委托方法中获取的:

optional func collectionView(_ collectionView: UICollectionView,
                  layout collectionViewLayout: UICollectionViewLayout,
             sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize

This is where you should be setting the size of your cells.

这是您应该设置单元格大小的地方。