Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

uicollectionview - 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()
}
question from:https://stackoverflow.com/questions/65894897/swift-uicollectionviewcell-uilabel-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()
    } 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...