0

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);
}
Community
  • 1
  • 1
Stine
  • 1,605
  • 5
  • 23
  • 44
  • It sounds like the right approach. Can you post your full code? If you wrap it in an animation block you can decide the desired end frame of your view before applying the transform. – EsbenB Jun 05 '15 at 08:44
  • Why are you not using `Autolayout` ? – Vinay Jain Jun 05 '15 at 08:55
  • I have added some more code, Esben :) Vinay, how? – Stine Jun 05 '15 at 09:10
  • I would recommend that you set the frame instead of the bounds. For more details see http://stackoverflow.com/questions/1071112/uiviews-frame-bounds-center-origin-when-to-use-what BUT I don't think this is causing your problems. From where are you invoking the animation block? perhaps you should try logging calls to screenWidth to see if it is actually called when you expect it to change orientation – EsbenB Jun 05 '15 at 09:27
  • @EsbenB: I just noticed that if I leave out the setFont line then the label size adjusts as expected - any idea why? – Stine Jun 05 '15 at 09:38

2 Answers2

1

Perhaps you are calling sizeToFit on the label somewhere?

Try instead to set:

myLabel.adjustsFontSizeToFitWidth  = YES;
myLabel.minimumFontScale      =  0.1; 

This will adjust your labels font size to fit the width of label.

EsbenB
  • 3,356
  • 25
  • 44
1

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

Stine
  • 1,605
  • 5
  • 23
  • 44