Noobass
Noobass

Reputation: 2014

UILabel Autolayout width trouble

So I'm trying to use autolayout for cell content view to get the proper layout. So my problem is that I have a UILabel that changes its size with respect to its text and I also have a UIView as a background view for this label with rounded corners. So my question is, how to force this UIView's width to be 10 points wider than the UILabel. I managed to make it the same width but how can I force it always to be a certain length wider?

Thank you in advance!

Upvotes: 2

Views: 1667

Answers (2)

Roshan
Roshan

Reputation: 1937

An autolayout constraint is nothing but an equation of the form

attribute1 == multiplier × attribute2 + constant

Note that programatically, you can virtually set any constraint on your views. The interface builder is however a bit limited given that you can relate only certain pairs of (attribute1,attribute2) an as you have noticed you may not be able to provide constant.

Have a look at https://developer.apple.com/library/ios/DOCUMENTATION/AppKit/Reference/NSLayoutConstraint_Class/NSLayoutConstraint/NSLayoutConstraint.html

Upvotes: 0

T. Benjamin Larsen
T. Benjamin Larsen

Reputation: 6383

NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:yourLabel
                                                                   attribute:NSLayoutAttributeWidth
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:yourLabel.superview
                                                                   attribute:NSLayoutAttributeWidth
                                                                  multiplier:1.0
                                                                    constant:10]; // <-- this
[yourLabel.superview addConstraint:widthConstraint];

Upvotes: 4

Related Questions