2011-07-08 51 views
1

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

UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation]; 

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

你是从视图控制器的viewDidLoad调用'[super viewDidLoad]'吗? – highlycaffeinated

+0

是的,我叫它,但它仍然是'0' – 1110

+0

如果你在你的'viewDidLoad'中向'[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]添加了一个调用,会发生什么? – highlycaffeinated

回答

3

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

+0

接缝这是真正的问题。如果应用程序已启动且设备未旋转,则方向未知。 Grrr ...任何人都有这个解决方案? – 1110

+0

因此,如果应用程序在桌面上启动,它将不会有方向设置,所以我接下来做了。如果方向未知,我设置视图元素位置,如果用户不喜欢他必须旋转设备。愚蠢的解决方案,但我没有看到另一种方式。 – 1110

0

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

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

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

2

您正在将UIDeviceOrientation类型投射到UIInterfaceOrientation类型。设备的方向有几个不同于接口方向的值。

尝试使用:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

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

编辑:如果设备的方向是未知的,你应该刚刚成立的正规纵向视图。

+0

当我这样做时,我得到:UIDeviceOrientationUnknown – 1110