【问题标题】:adding subviews in uitableviewcell breaks constraints在 uitableviewcell 中添加子视图会破坏约束
【发布时间】:2019-10-18 02:48:10
【问题描述】:

我有一个固定高度和宽度为 55 的图像视图和一个标签,但我收到以下错误。即使我设置了 UITableView.automaticDimension

,约束也无法告诉 tableview 高度应该是多少
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
    }

【问题讨论】:

    标签: ios swift uitableview ios-autolayout


    【解决方案1】:

    您忘记在标签上设置translatesAutoresizingMaskIntoConstraints = false — 正如错误消息非常清楚地指出的那样。

    【讨论】:

    • 我也经常忘记!初学者的错误是没有阅读错误信息。
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多