我有一个固定高度和宽度为55的imageview,下面有一个标签,但出现以下错误。约束无法告诉tableview即使我设置了UITableView.automaticDimension甚至应该考虑的高度
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x2811767b0 h=--& v=--& UILabel:0x1057292b0'steve_rodriquez'.minY == 0 (active, names: '|':UITableViewCellContentView:0x105729520 )>",
"<NSAutoresizingMaskLayoutConstraint:0x281176800 h=--& v=--& UILabel:0x1057292b0'steve_rodriquez'.height == 0 (active)>",
"<NSLayoutConstraint:0x2811765d0 UILabel:0x1057292b0'steve_rodriquez'.bottom == UITableViewCellContentView:0x105729520.bottom (active)>",
"<NSLayoutConstraint:0x2811772a0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x105729520.height == 0.333333 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x2811765d0 UILabel:0x1057292b0'steve_rodriquez'.bottom == UITableViewCellContentView:0x105729520.bottom (active)>
我的tableviewcell类是有约束的
class ChatTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(userImageView)
self.contentView.addSubview(username)
userImageView_constraints()
username_constraints()
}
var userImageView: UIImageView = {
let imageView = UIImageView()
let imageViewHeightAndWidth: CGFloat = 55
let image = UIImage(named: "steve")
imageView.image = image
imageView.clipsToBounds = true
imageView.layer.cornerRadius = imageViewHeightAndWidth / 2
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
var username: UILabel = {
let label = UILabel()
label.text = "steve_rodriquez"
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 13)
return label
}()
func userImageView_constraints(){
userImageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 20).isActive = true
userImageView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive = true
userImageView.widthAnchor.constraint(equalToConstant: 55).isActive=true
userImageView.heightAnchor.constraint(equalToConstant: 55).isActive=true
}
func username_constraints(){
username.topAnchor.constraint(equalTo: userImageView.bottomAnchor, constant: 0).isActive = true
username.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0).isActive=true
username.centerXAnchor.constraint(equalTo: userImageView.centerXAnchor).isActive=true
username.widthAnchor.constraint(equalToConstant: 65).isActive=true
}
答案 0 :(得分:1)
您忘记在标签上设置translatesAutoresizingMaskIntoConstraints = false
-因为错误消息很清楚地指出了这一点。