Search code examples
swiftuicollectionviewuilabel

Swift UICollectionViewCell UIlabel issue


I am writing a calendar, and each day is a cell, each cell has a Rounded UILabel in contentView, but I don't know why is there the little black border on each cell Calendar image

In 3d View 3d preview

class CalendarCell: UICollectionViewCell {

static var identifier: String = "DayCell"
let dayLabel: UILabel = UILabel()


override init(frame: CGRect) {
    super.init(frame: frame)
    self.setUpUI()
    self.contentView.addSubview(dayLabel)
}

private func setUpUI() {
    dayLabel.text = nil
    dayLabel.sizeToFit()
    dayLabel.backgroundColor = .white
    //dayLabel.layer.borderWidth = 0.5
    
    dayLabel.textColor = .black
    dayLabel.textAlignment = .center
    dayLabel.clipsToBounds = true
    
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
    dayLabel.frame = self.contentView.frame
    dayLabel.layer.cornerRadius = dayLabel.frame.width / 2
}

override func prepareForReuse() {
    super.prepareForReuse()
    setUpUI()
}

Solution

  • I'm not sure what's causing the problem but I'm pretty sure you can fix it and achieve the same behavior by changing your code to this:

    let collectionViewCellWidth: CGFLoat = 150 // or whatever you want. You'd define this in the file with your custom flow layout or wherever your give the cell size to the collectionView.
    class CalendarCell: UICollectionViewCell {
        static let identifier = "DayCell" // type inference doesn't need the annotations on these two
        let dayLabel = UILabel()
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            setUpUI()
        }
    
        private func setUpUI() {
            contentView.layer.cornerRadius = collectionViewCellWidth / 2
            contentView.clipsToBounds = true
            contentView.backgroundColor = .white // or orange, whatever
    
            dayLabel.text = nil
            dayLabel.backgroundColor = .white
            //dayLabel.layer.borderWidth = 0.5
        
            dayLabel.textColor = .black
            dayLabel.textAlignment = .center
            dayLabel.translatesAutoresizingMaskIntoConstraints = false
            
            contentView.addSubview(dayLabel)
    
            NSLayoutConstraint.activate([
                dayLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
                dayLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor)
            ])
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        //override func layoutSubviews() {
        //    dayLabel.frame = self.contentView.frame
        //    dayLabel.layer.cornerRadius = dayLabel.frame.width / 2
        //}
        
        // also as your code currently is, you don't do anything in your setup function that needs to be redone when a cell is dequeued for reuse. Unless you were setting some unique information for a cell like its color or text. Just FYI
        override func prepareForReuse() {
            super.prepareForReuse()
            setUpUI()
        } 
    }