Reputation: 29316
I want to have a two-line UILabel at the bottom of my UITextFields, acting as a disclaimer.
I have the following code in viewDidLoad
:
UILabel *InstapaperSubscriptionLabel = [[UILabel alloc] init];
InstapaperSubscriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
InstapaperSubscriptionLabel.text = @"Requires an Instapaper subscription, please see the Instapaper website for more details.";
InstapaperSubscriptionLabel.numberOfLines = 2;
InstapaperSubscriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
InstapaperSubscriptionLabel.preferredMaxLayoutWidth = self.view.bounds.size.width - 40;
InstapaperSubscriptionLabel.textAlignment = NSTextAlignmentCenter;
InstapaperSubscriptionLabel.backgroundColor = [UIColor clearColor];
InstapaperSubscriptionLabel.textColor = [UIColor colorWithRed:112/255.0 green:112/255.0 blue:111/255.0 alpha:1.0];
InstapaperSubscriptionLabel.font = [UIFont systemFontOfSize:14.0];
[self.view addSubview:InstapaperSubscriptionLabel];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:InstapaperSubscriptionLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.passwordBox
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:15.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:InstapaperSubscriptionLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:20.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:InstapaperSubscriptionLabel
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:20.0]];
But each time I load the view, it looks like this:
Where it goes off the side of the screen. Why is it doing this? I want it more centered.
Upvotes: 1
Views: 709
Reputation: 104082
I think the constant for the last constraint you show in your question should be -20, not 20. What you're saying with that code is:
label trailing = superview right * 1 + 20 which will put the trailing edge of the label 20 points off the screen to the right.
Upvotes: 2