如何解决向UICollectionViewCell
我正在用代码编写UI,并且正在尝试为UICollectionViewCell
编写代码并向其中添加UIImageView
。
我需要在代码中创建UIImageView
约束。
在UIICollectionViewCell
中创建imageView和约束的最佳实践是什么? e.i-哪种UIICollectionViewCell
创建方法?
解决方法
这是我习惯于做您要问的方式:
-
创建我的 UICollectionViewController 类:
class MyCollectionController: UICollectionViewController { let cellId = "cellId" override func viewDidLoad() { super.viewDidLoad() collectionView.backgroundColor = .red } override func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int { return 10 } }
-
创建您的自定义单元格,我们称之为 MyCollectionCell :
class MyCollectionCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) backgroundColor = .yellow } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
-
以编程方式声明您的UIImageView(我更喜欢在init函数之前):
let imageView: UIImageView = { let iv = UIImageView() iv.backgroundColor = .blue return iv }()
别忘了()确保代码块被调用!
- 我们需要将imageView添加到单元格视图中并将其锚定
为此,我总是在UIView上使用扩展,其中包含一个名为 anchor
的函数。extension UIView {
func anchor(top: NSLayoutYAxisAnchor?,left: NSLayoutXAxisAnchor?,bottom: NSLayoutYAxisAnchor?,right: NSLayoutXAxisAnchor?,topPadding : CGFloat,leftPadding: CGFloat,bottomPadding: CGFloat,rightPadding: CGFloat,width: CGFloat,height: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
if let top = top {
self.topAnchor.constraint(equalTo: top,constant: topPadding).isActive = true
}
if let left = left {
self.leftAnchor.constraint(equalTo: left,constant: leftPadding).isActive = true
}
if let bottom = bottom {
self.bottomAnchor.constraint(equalTo: bottom,constant: -bottomPadding).isActive = true
}
if let right = right {
self.rightAnchor.constraint(equalTo: right,constant: -rightPadding).isActive = true
}
if width != 0 {
self.widthAnchor.constraint(equalToConstant: width).isActive = true
}
if height != 0 {
self.heightAnchor.constraint(equalToConstant: height).isActive = true
}
}
}
现在,我们可以添加imageView并将其锚定在MyCollectionCell的init方法中:
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .yellow
addSubview(imageView)
imageView.anchor(top: topAnchor,left: leftAnchor,bottom: bottomAnchor,right: rightAnchor,topPadding: 3,leftPadding: 3,bottomPadding: 3,rightPadding: 3,width: 0,height: 0)
}
- 让我们使用MyCollectionCell作为MyCollectionController的cellForItemAt
在viewDidLoad中,使用正确的类(MyCollectionCell)注册单元格
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView.backgroundColor = .red
collectionView.register(MyCollectionCell.self,forCellWithReuseIdentifier: cellId)
}
将单元格列为MyCollectionCell
override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId,for: indexPath) as! MyCollectionCell
return cell
}
- 介绍MyCollectionController时,您应该看到以下结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。