Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - How do I set the correct constraints for the table view below?

I need help setting the correct constraints for the table view below. Heres the code for the constraints.enter image description here. I am trying to show a 60 by 60 image view with a text by the right; please have a look at Tvtable view cell file. Project link: https://github.com/lexypaul13/Trending-Tv-Shows/

private func set() {
    addSubview(tvImage)
    addSubview(tvName)
    
    tvImage.translatesAutoresizingMaskIntoConstraints = false
    tvName.translatesAutoresizingMaskIntoConstraints = false
    let padding: CGFloat    = 12

    NSLayoutConstraint.activate([
        tvImage.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
        tvImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: padding),
        tvImage.heightAnchor.constraint(equalToConstant: 60),
        tvImage.widthAnchor.constraint(equalToConstant: 60),
        
        tvName.centerYAnchor.constraint(equalTo: self.centerYAnchor),
        tvName.leadingAnchor.constraint(equalTo: tvImage.trailingAnchor, constant: 24),
        tvImage.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -padding),
        tvImage.heightAnchor.constraint(equalToConstant: 40)])
}
question from:https://stackoverflow.com/questions/65880238/how-do-i-set-the-correct-constraints-for-the-table-view-below

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

1 Answer

0 votes
by (71.8m points)

I think the first problem is that you should place the subviews of a table view cell into it's content view, so use:

self.contentView.addSubview(tvImage)
self.contentView.addSubview(tvName)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...