iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 创建UICollectionViewCell时子视图框不正确大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

问题 我创建了一个带有自定义UICollectionViewCell的UICollectionViewController。 自定义单元格包含一个大的矩形UIView(名为colorView)和一个UILabel(名为nameLabel)。 当集合首先填充其单元格并打印colorView.frame时,打印的框架具有不正确的值。我知道它们不正确,因为colorView框架大于单元格框架,即使col
问题

我创建了一个带有自定义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! CustomcatollectionViewCell
Cell.setNeedsLayout()
Cell.layoutIfNeeded()

大佬总结

以上是大佬教程为你收集整理的ios – 创建UICollectionViewCell时子视图框不正确全部内容,希望文章能够帮你解决ios – 创建UICollectionViewCell时子视图框不正确所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。