Search code examples
iosswiftxcodeuilabelnslayoutconstraint

Programmatic constraints cut the bottom of my label off


I have a Nib file with a root UITableViewCell and child UILabel that I anchor at run time using programmatic constraints

        lblAccountItemTitle.translatesAutoresizingMaskIntoConstraints = false
        lblAccountItemTitle.topAnchor.constraint(lessThanOrEqualTo: self.topAnchor, constant: 16).isActive = true
        lblAccountItemTitle.bottomAnchor.constraint(lessThanOrEqualTo: self.bottomAnchor, constant: -16).isActive = true
        lblAccountItemTitle.leadingAnchor.constraint(equalTo: imgAccountItemLeft.trailingAnchor, constant: 16).isActive = true
        lblAccountItemTitle.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -32).isActive = true
        lblAccountItemTitle.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true

enter image description here

Also, I've noticed that the shorter I make my bottom anchor, the less clipped the text is

How can I get rid of the clipping while still maintaining the equal 16 vertical padding?


Solution

  • I was programmatically pinning the label to the cell and not to the Content View

    self.topAnchor

    should've been

    self.contentView.topAnchor