在viewDidLoad中错误地报告了方向 [英] orientation incorrectly reported in viewDidLoad

查看:60
本文介绍了在viewDidLoad中错误地报告了方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个应用需要加载不同的图像作为背景图像,具体取决于设备的方向。我在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"];        
}

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

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?

在shouldAutoRotateForInterfaceOrientation中我有以下内容:

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.

推荐答案

原因是 viewDidLoad 太早了。每个应用程序都以纵向方式启动,然后旋转到横向。当调用 viewDidLoad 时,尚未发生轮换。您希望使用延迟性能,或将测试放在 didRotateFromInterfaceOrientation 或类似内容中。请参阅我的书中的解释:

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

这篇关于在viewDidLoad中错误地报告了方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆