Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

iphone - orientation incorrectly reported in viewDidLoad

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason is that viewDidLoad is too early. Every app launches in portrait and later rotates to landscape. When viewDidLoad is called, the rotation has not happened yet. You want to use delayed performance, or put your tests in didRotateFromInterfaceOrientation or similar. See the explanation in my book:

http://www.apeth.com/iOSBook/ch19.html#_rotation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...