iosswiftautolayoutconstraintscustom-cell

Custom UITableViewCell with title - constraints


I have a custom UITableViewCell so that

class CustomCell: UITableViewCell {
    private var someCustomView = UIView()

    init(style: UITableViewCell.CellStyle, reuseIdentifier: String?, text: String?) {
      super.init(style: style, reuseIdentifier: reuseIdentifier) 
      setupContraints()
   }

  required init?(coder: NSCoder) {
     super.init(coder: coder)
  }

  private func setupContraints() {
      contentView.addSubview(someCustomView)

      self.someCustomView.frame.size = CGSize(width: 100, height: 100)

      NSLayoutConstraint.activate([
        self.someCustomView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        self.someCustomView.topAnchor.constraint(equalTo: contentView.topAnchor)
     ])

    setNeedsLayout()
    layoutIfNeeded()

}

The issue I am having is around the NSLayoutConstraint - Ignore the specifics of this as I haven't yet setup the constraints I need, however the view only setting up the frame and not constraints. Any help? Thank you!


Solution

  • You need to set translatesAutoresizingMaskIntoConstraints to false before adding constraints as follows:

    someCustomView.translatesAutoresizingMaskIntoConstraints = false