Search code examples
iosobjective-cuiinterfaceorientationcgaffinetransformorientation-changes

Manually rotate and resize UILabel on orientation change


In my previous question I shared my problem with the black background appearing in my app on orientation change: Black background when view rotates on orientation change

I did not manage to solve the problem by following any of the advices I got and I have the feeling that the only way I can avoid the black background is by manually rotating my subviews on orientation change?

One of my subviews is a UILabel which is supposed to cover the entire screen. The rotation is going pretty well using a line of code similar to this one:

myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));

My problem is to make the UILabel adjust to take up the entire screen in landscape mode as well. I have tried to switch height and width of its bounds, but nothing happens.

Any suggestions will be greatly appreciated.


Here are some more code details:

[UIView animateWithDuration:0.5
                 animations:^{
                     myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
                 }
                 completion:^(BOOL finished) {
                     myLabel.bounds = CGRectMake(0, 0, [self screenWidth], [self screenHeight]);

                     CGFloat fontSize = ((isLandscape ? 0.9649 : 0.9375) * [self screenWidth]) / 2.74;
                     [myLabel setFont:[UIFont fontWithName:FontName size:fontSize]];
                 }
];

where

- (CGFloat) screenWidth {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    return isLandscape ? MAX(screenSize.width, screenSize.height) : MIN(screenSize.width, screenSize.height);
}

- (CGFloat) screenHeight {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    return isLandscape ? MIN(screenSize.width, screenSize.height) : MAX(screenSize.width, screenSize.height);
}

Solution

  • I found out that I was able to adjust the label size once I unchecked the auto layout checkbox.