добавление подпредставлений в ограничениях uitableviewcell - PullRequest
0 голосов
/ 18 октября 2019

У меня есть изображение с фиксированной высотой и шириной 55 и меткой под ним, но я получаю следующую ошибку. Ограничения не могут сказать табличному виду, какую высоту нужно даже думать, если я установил 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
    }

1 Ответ

1 голос
/ 18 октября 2019

Вы забыли установить translatesAutoresizingMaskIntoConstraints = false на этикетке - как ясно указывает сообщение об ошибке.

...