2010-12-12 59 views
19

这里是一个noob问题。如何处理UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown?

我检测方位:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

一切都很好,很正常,我重新定位我的文本字段和标签根据上报的定向

if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)

和else { } for everything else

我最近才发现的问题是报告UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown。我该如何处理这种情况?我如何知道纵向或横向是否出现UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown?我知道设备朝上或朝下,但我不知道是否应将所有内容重新定位到纵向或横向。

谢谢!

回答

33

Apple建议不要使用设备方向进行视图布局。相反,每个视图控制器具有interfaceOrientation属性,而UIApplication具有statusBarOrientation属性,这两个属性将返回当前的接口方向,这适合视图布局。

要监视更改,将会调用方法(如willRotateToInterfaceOrientation:duration:),并会发布诸如UIApplicationWillChangeStatusBarOrientationNotification之类的通知,这些通知将在发生界面方向更改时发布。

+0

谢谢,这看起来很有前途。我会看看 – TrekOnTV2017 2010-12-12 19:15:59

+0

这是正确答案,只是重构UIDevice方向响应(如博客所示)不如听取UIViewController的interfaceOrientation。工作很好。 – Jessedc 2011-04-27 03:43:13

+0

我更喜欢这个答案。 caprica13,你能把这个标记为正确吗? – 2011-12-18 00:02:30

4

此博客帖子在这里:http://blogs.remobjects.com/blogs/mh/2010/06/29/p1685解释得很好。

+0

完美!我做了改变,现在一切似乎都很顺利。谢谢。 – TrekOnTV2017 2010-12-12 20:12:58

+0

这个怪异的主题让我头痛,gah – Alex 2012-01-17 18:05:15

+0

哈哈哈想象一下,如果Apple的Objective-C/frameworks文档看起来像这样! – 2012-01-17 20:37:24

-1

我有同样的问题

[[的UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

该方法产生用于7个可能的设备位置状态通知:

UIDeviceOrientationPortrait 
UIDeviceOrientationPortraitUpsideDown 
UIDeviceOrientationLandscapeLeft 
UIDeviceOrientationLandscapeRight 
UIDeviceOrientationFaceUp 
UIDeviceOrientationFaceDown 
UIDeviceOrientationUnknown 

但我只需要前4,以及这是我的方法来解决这个问题(与包括代码): How to handle UIDeviceOrientation for manage views layouts

0

有点晚了,希望它对一个人有帮助。

对于iOS 6.0或更高版本:

1)您查看安装过程中设置此代码,以接收旋转通知:

// Set Listener to receive orientation notifications from the device 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(detectOrientation) 
               name:UIDeviceOrientationDidChangeNotification 
              object:nil]; 

2)设置此代码来捕获旋转后的通知:

-(void)detectOrientation{  
    switch ([[UIDevice currentDevice] orientation]) { 
    case UIDeviceOrientationFaceDown:{ 
     NSLog(@"UIDeviceOrientationFaceDown"); 
     break; 
    } 
    case UIDeviceOrientationFaceUp:{ 
     NSLog(@"UIDeviceOrientationFaceUp"); 
     break; 
    } 
    case UIDeviceOrientationLandscapeLeft:{ 
     NSLog(@"UIDeviceOrientationLandscapeLeft"); 
     break; 
    } 
    case UIDeviceOrientationLandscapeRight:{ 
     NSLog(@"UIDeviceOrientationLandscapeRight"); 
     break; 
    } 
    case UIDeviceOrientationPortrait:{ 
     NSLog(@"UIDeviceOrientationPortrait"); 
     break; 
    } 
    case UIDeviceOrientationPortraitUpsideDown:{ 
     NSLog(@"UIDeviceOrientationPortraitUpsideDown"); 
     break; 
    } 
    case UIDeviceOrientationUnknown:{ 
     NSLog(@"UIDeviceOrientationUnknown"); 
     break; 
    } 
    default:{ 
     break; 
    } 
    } 

}