在viewDidLoad中错误地报告了方向

Fre*_*nda 3 iphone objective-c ipad ios

所以我有一个应用程序需要加载不同的图像作为背景图像取决于设备的方向.我在viewDidLoad中有以下代码:

BOOL isLandScape = UIDeviceOrientationIsLandscape(self.interfaceOrientation);

if (isLandScape)
{
    self.bgImage.image = [UIImage imageNamed:@"login_bg748.png"];        
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,即使模拟器在横向开始,这个bool仍然是假的.我检查了它,它总是报告处于纵向模式,无论实际的模拟器方向如何.有没有人知道为什么这不起作用?

在shouldAutoRotateForInterfaceOrientation中,我有以下内容:

if (UIDeviceOrientationIsLandscape(interfaceOrientation))
    {
        self.bgImage.image = [UIImage imageNamed:@"login_bg748.png"];
    } else
    {
        self.bgImage.image = [UIImage imageNamed:@"login_bg1004.png"];        
    }

    return YES;
Run Code Online (Sandbox Code Playgroud)

而且这段代码确实有效,它只是搞砸的初创公司.在我执行一次旋转后,它工作正常.

mat*_*att 7

原因是viewDidLoad为时尚早.每个应用程序都以纵向方式启动,然后旋转到横向.当viewDidLoad被调用时,旋转还没有发生.您希望使用延迟性能,或者将测试用于didRotateFromInterfaceOrientation或类似.请参阅我的书中的解释:

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