Take the 2-minute tour ×

I have a view controller which has two UILabels and a UIButton centered and stacked at the bottom. The UILabels have 20 pixels between their outer edges and the border of the view, and there are 10 pixels between each control.

When I rotate the device to a landscape orientation, the labels stay centered, and the padding and spacing stays consistent, but the heights of the labels do not change - if the label could previously contain three lines, it is still three lines high, despite the text being only two lines in the new orientation

I've seen similar questions on Stack Overflow with instructions on how to find and change the label height, but these seem to leave the upper left hand corner of the label where it would be if the label were still a larger height. This is despite constraints which specifically state there should only be 10 pixels between the two labels. I'm assuming this is because the solutions I've found change the height of the frame, but this also forces the origin to remain the same.

I can add more code to calculate the new origin of the label, in addition to calculating the height and setting the new correct width, but for something which must be reasonably common, there has to be an easier way.

Is there any way to have labels follow constraints, and ignore their set origin? Or have labels change size and position properly to fit their text?

Here's the code I'm currently using:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    CGSize maximumLabelSize = CGSizeMake(self.view.frame.size.width - 40, CGFLOAT_MAX);

    CGSize expectedLabelSize = [helpMessageText sizeWithFont:[helpMessageLabel font] constrainedToSize:maximumLabelSize lineBreakMode:[helpMessageLabel lineBreakMode]];
    CGRect newFrame = helpMessageLabel.frame;
    newFrame.size.height = expectedLabelSize.height;
    [helpMessageLabel setText:helpMessageText];
    [helpMessageLabel setFrame:newFrame];
    NSLog(@"%f", newFrame.size.height);
    [helpMessageLabel setBackgroundColor:[UIColor blueColor]];

    expectedLabelSize = [errorMessageText sizeWithFont:[errorMessageLabel font] constrainedToSize:maximumLabelSize lineBreakMode:[errorMessageLabel lineBreakMode]];
    newFrame = errorMessageLabel.frame;
    newFrame.size.height = expectedLabelSize.height;
    [errorMessageLabel setText:errorMessageText];
    errorMessageLabel.frame = newFrame;
    NSLog(@"%f", newFrame.size.height);
    [errorMessageLabel setBackgroundColor:[UIColor greenColor]];

Here's what the app looks like with no special code: Here's what the app looks like with no special code

Here's what the app looks like in landscape with the above code: Here's what the app looks like in landscape with the above code!

Here's closer to what I'd like it to look like: Here's closer to what I'd like it to look like

share|improve this question
How often have you seen an "expected error" shown to a user? The word 'unexpected' seems completely superfluous. –  occulus Nov 22 '12 at 16:40
have u looked at the tutorial by raywenderlich for autolayout.. raywenderlich.com/20881/beginning-auto-layout-part-1-of-2 –  vishy Nov 22 '12 at 17:18
Haha, yeah. That text is actually a bad example. It can only ever be displayed while debugging. The same loading screen has other, more helpful, text passed to it through the segue depending on the error. –  Steven Collins Nov 22 '12 at 17:31
I have looked at that tutorial. I do have constraints set up, but I believe they get ignored when setting the frame in code. I don't know a way of changing the size of the label without setting it through the frame, which happens to set the origin as well. –  Steven Collins Nov 22 '12 at 17:34
Have you looked at UIAlertView? –  WolfLink Nov 22 '12 at 18:27
show 1 more comment


Your Answer



Browse other questions tagged or ask your own question.