ios – 如何以编程方式在Swift 3中将UILabel添加到UICollectionViewCell

前端之家收集整理的这篇文章主要介绍了ios – 如何以编程方式在Swift 3中将UILabel添加到UICollectionViewCell前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有几个UICollectionViewCells,我想以编程方式向单元格添加两个标签.我怎么能以正确的方式做到这一点?

解决方法

在您的单元类中,将其定义为全局.

class YourCollectionViewCell: UICollectionViewCell {

        var titleLabel:UILabel = {
            let label = UILabel(frame: CGRect(x:100,y: 30,width: UIScreen.main.bounds.width,height: 40))
            label.textAlignment = .left
            label.lineBreakMode = .byWordWrapping
            label.numberOfLines = 0
            return label
        }()

        override init(frame: CGRect) {
            super.init(frame: frame)
            self.addSubview(self.titleLabel)
        }
}

猜你在找的iOS相关文章