加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 创建UICollectionViewCell时子视图框不正确

发布时间:2020-12-15 02:00:55 所属栏目:百科 来源:网络整理
导读:问题 我创建了一个带有自定义UICollectionViewCell的UICollectionViewController。 自定义单元格包含一个大的矩形UIView(名为colorView)和一个UILabel(名为nameLabel)。 当集合首先填充其单元格并打印colorView.frame时,打印的框架具有不正确的值。我知道它
问题

我创建了一个带有自定义UICollectionViewCell的UICollectionViewController。
自定义单元格包含一个大的矩形UIView(名为colorView)和一个UILabel(名为nameLabel)。

当集合首先填充其单元格并打印colorView.frame时,打印的框架具有不正确的值。我知道它们不正确,因为colorView框架大于单元格框架,即使colorView被正确绘制。

但是,如果我将collectionView滚动到足以触发先前创建的单元格的重用,那么colorView.frame现在具有正确的值!
我需要正确的框架,因为我想将圆角应用于colorView层,为此需要正确的coloView大小。
顺便说一句,如果你想知道,colorView.bounds也有与colorView.frame相同的错误的大小值。

问题

为什么在创建单元格时框架不正确?

现在还有一些代码

这是我的UICollectionViewCell:

class BugCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var colorView: UIView!
    @IBOutlet weak var nameLabel: UILabel!
}

这是UICollectionViewController:

import UIKit

let reuseIdentifier = "Cell"
let colors = [UIColor.redColor(),UIColor.blueColor(),UIColor.greenColor(),UIColor.purpleColor()]
let labels = ["red","blue","green","purple"]

class BugCollectionViewController: UICollectionViewController,UICollectionViewDelegateFlowLayout {
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return colors.count
    }

    override func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,forIndexPath: indexPath) as BugCollectionViewCell

        println("ColorView frame: (cell.colorView.frame) Cell frame: (cell.frame)")

        cell.colorView.backgroundColor = colors[indexPath.row]
        cell.nameLabel.text = labels[indexPath.row]

        return cell
    }

    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let width = self.collectionView?.frame.width
        let height = self.collectionView?.frame.height
        return CGSizeMake(width!,height!/2)
    }
}

设置集合视图以便一次显示两个单元格,垂直方向,每个单元格都包含一个涂有颜色的大矩形和带颜色名称的标签。

当我在模拟器上运行上面的代码时,我得到以下打印结果:

ColorView frame: (0.0,0.0,320.0,568.0) Cell frame: (0.0,375.0,333.5)
ColorView frame: (0.0,343.5,333.5)

这是一个奇怪的结果 – colorView.frame的高度为568分,而单元格框架只有333.5分高。

如果我将collectionView拖动并重新使用单元格,则打印出以下结果:

ColorView frame: (8.0,8.0,359.0,294.0) Cell frame: (0.0,1030.5,333.5)
ColorView frame: (8.0,333.5)

一些我不明白的东西发生在纠正colorView框架的方式。

我认为它与从Nib加载单元格的事实有关,所以不用使用init(frame:frame)初始化器,控制器使用init(coder:aCoder)初始化器,所以一旦单元格创建它可能带有一些默认框架,我无法编辑。

我会感谢任何帮助,让我了解发生了什么!

我正在使用Xcode 6.1.1。与iOS SDK 8.1。

解决方法

您可以通过在您的自定义Cell类中覆盖layoutIfNeeded()来获得单元格的最终帧,如下所示:

override func layoutIfNeeded() {
    super.layoutIfNeeded()
    self.subView.layer.cornerRadius = self.subView.bounds.width / 2
}

然后在你的UICollectionView数据源方法cellForRowAtIndexPath:这样做:

let Cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath) as! CustomCollectionViewCell
Cell.setNeedsLayout()
Cell.layoutIfNeeded()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读