【问题标题】:iPad Interface Orientation ProblemiPad界面方向问题
【发布时间】:2011-07-09 14:49:55
【问题描述】:

当用户点击 UIView(全屏)时,我的应用需要检测方向并执行一些操作。但是有一个小问题。 如果我以横向模式启动应用程序并且用户点击背景“interfaceOrientation”变量为“0”并且我不知道如何重新排列视图元素。如果我在一切正常后旋转模拟器,但如果不是,则“interfaceOrientation”为“0”。在这里做什么?

UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];

if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    {        
       ...            
    }  
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {       
        ...
    }
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
...
}

【问题讨论】:

  • 您是从视图控制器的viewDidLoad 调用[super viewDidLoad] 吗?
  • 是的,我称之为,但它仍然是 '0'
  • 如果您在viewDidLoad 中添加对[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] 的调用会怎样?

标签: iphone objective-c ios ipad ios4


【解决方案1】:

我不确定这解决得有多好。 UIDeviceOrientationUnknown 为 0。这意味着,特别是对于没有陀螺仪的 iPad,此时的方向根本不知道。想象一下您的 iPad 平放在桌子上,用户正在启动您的应用程序:没有任何方法可以定义您的应用程序实际上是横向还是纵向运行,除非您相应地倾斜设备。因此.. 启动时的方向始​​终为 0(未知)。

【讨论】:

  • 看来这是真正的问题。如果应用程序已启动且设备未旋转,则方向未知。 Grrr ...有人对此有解决方案吗?
  • 因此,如果应用程序在桌面上启动,它不会设置方向,所以我接下来做了。如果方向未知,我设置视图元素位置,如果用户不喜欢他必须旋转设备。愚蠢的解决方案,但我没有看到其他方法。
【解决方案2】:

您正在将 UIDeviceOrientation 类型转换为 UIInterfaceOrientation 类型。设备方向除了接口方向之外还有几个不同的值。

尝试使用:

 UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

switch (deviceOrientation) {
    default:
    case UIDeviceOrientationPortrait:
    case UIInterfaceOrientationPortraitUpsideDown:
    case UIDeviceOrientationUnknown:
        //...
        break;
    case UIDeviceOrientationLandscapeLeft:
        //...
        break;
    case UIDeviceOrientationLandscapeRight:
        //...
        break;
}

编辑:如果设备方向未知,您应该设置常规纵向视图。

【讨论】:

  • 当我这样做时,我得到:UIDeviceOrientationUnknown
【解决方案3】:

如果您无法从视图控制器访问 self.interfaceOrientation,您应该能够访问 [UIApplication sharedApplication].statusBarOrientation

另外,有时在viewDidLoad 中调用self.interfaceOrientation 是有风险的,因为视图会在它意识到它的方向之前加载,所以它会在之后执行旋转。在这种情况下,请尝试在 viewWillAppear 中找到它。

或者只是覆盖willRotateToInterfaceOrientation:duration 并将该信息传递给需要它的 UIView。注意:如果您将 xib 设置为该方向,则不会调用它。

【讨论】:

    猜你喜欢
    • 2011-10-22
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多