I am programmatically building a view and using autolayout, no interface builder at all. In a custom ScrollView controller, I am adding a UILabel and a UIButton as subviews. I w
You don't have enough constraints for the button. Actually, you haven't got enough constraints for the label either, but that won't necessarily stop your button aligning to the right of the screen.
You always must have constraints which specify how a view behaves in both horizontally and vertically. Usually this means a minimum of four constraints - x, y, width and height. If you don't, iOS will guess for you and chances are it'll be wrong. You'll probably notice a few warnings get thrown up in the Xcode console.
For this problem, it really depends on exactly how you want your view to look, but the simplest solution is to just set a width constraint for the button. You can do that like this:
[NSLayoutConstraint constraintWithItem:favouriteButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:64.0];
Where 64.0f if the width of the button.
If you want the button to be to the right of the label and share the same width (ie, each taking up 50% of the screen), you can do this:
[NSLayoutConstraint constraintWithItem:favouriteButton
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:priceLabel
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.0f];
[NSLayoutConstraint constraintWithItem:favouriteButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:priceLabel
attribute:NSLayoutAttributeWidth
multiplier:1.0f
constant:0.0f];
All the above two constraints do is set the trailing edge of the label to the leading edge of the button and then says that both views must be the same width.
You should also really add height constraints to both views (or top and bottom constraints). I know you've got a vertical centering constraint, but that doesn't specify its size. iOS may work it out by content height, but chances are it'll throw up warnings in the console.