So I have an app that needs to load a different image as the background image depending on the orientation of the device. I have the following code in viewDidLoad:
BOOL isLandScape = UIDeviceOrientationIsLandscape(self.interfaceOrientation);
if (isLandScape)
{
self.bgImage.image = [UIImage imageNamed:@"login_bg748.png"];
}
For some reason even if the simulator starts in landscape this bool is still false. I checked and it always reports being in portrait mode regardless of the actual simulator orientation. Does anyone have an idea as to why this is not working?
In shouldAutoRotateForInterfaceOrientation I have the following:
if (UIDeviceOrientationIsLandscape(interfaceOrientation))
{
self.bgImage.image = [UIImage imageNamed:@"login_bg748.png"];
} else
{
self.bgImage.image = [UIImage imageNamed:@"login_bg1004.png"];
}
return YES;
And this code does work, its just the startup that is messed up. After I perform one rotation it works fine.
The reason is that
viewDidLoadis too early. Every app launches in portrait and later rotates to landscape. WhenviewDidLoadis called, the rotation has not happened yet. You want to use delayed performance, or put your tests indidRotateFromInterfaceOrientationor similar. See the explanation in my book:http://www.apeth.com/iOSBook/ch19.html#_rotation