Swift之自定义UICollectionViewCell

前端之家收集整理的这篇文章主要介绍了Swift之自定义UICollectionViewCell前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

自定义UICollectionViewCell和自定义UITableViewCell差不多,不过自定义UICollectionViewCell更像自定义UIView,具体代码如下

import UIKit

class ClassifyCollectionViewCell: UICollectionViewCell {

    
   var imageView: UIImageView!
    
   var titleLabel: UILabel!
    
    override init(frame:CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        imageView = UIImageView()
        
        titleLabel = UILabel()
        titleLabel.font = UIFont.systemFont(ofSize: 13)
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.black
        
        
        self.addSubview(imageView)
        self.addSubview(titleLabel)
        
        self.backgroundColor = UIColor.white
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let frame:CGRect = self.bounds
        let imgx:CGFloat = 5.0
        let imgy = imgx
        
        let frameWidth:CGFloat = frame.size.width
        let imgWidth:CGFloat = frameWidth - (imgx * 2.0)
        
        
        self.imageView.frame = CGRect(x: imgx,y: imgy,width: imgWidth,height: imgWidth)
        
        self.titleLabel.frame = CGRect(x: 0,y:imgy+frameWidth,width: frameWidth,height: 20)
        
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

}
原文链接:https://www.f2er.com/swift/321602.html

猜你在找的Swift相关文章