向UICollectionViewCell

问题描述

我正在用代码编写UI,并且正在尝试为UICollectionViewCell编写代码并向其中添加UIImageView

我需要在代码中创建UIImageView约束。

UIICollectionViewCell中创建imageView和约束的最佳实践是什么? e.i-哪种UIICollectionViewCell创建方法

解决方法

这是我习惯于做您要问的方式:

  1. 创建我的 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
         }
    
    
     }
    
  2. 创建您的自定义单元格,我们称之为 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")
         }
     }
    
  3. 以编程方式声明您的UIImageView(我更喜欢在init函数之前):

     let imageView: UIImageView = {
         let iv = UIImageView()
         iv.backgroundColor = .blue
         return iv
     }()
    

别忘了()确保代码块被调用!

  1. 我们需要将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)
    }
  1. 让我们使用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
    }
  1. 介绍MyCollectionController时,您应该看到以下结果:

Expected result