19

这里有一个菜鸟问题。

我用以下方法检测方向:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

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

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

其他{}

我最近才发现的问题是UIDevice报告UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown. 我该如何处理这种情况?我如何知道肖像或风景中是否UIDeviceOrientationFaceUp正在UIDeviceOrientationFaceDown发生?我知道设备是朝上还是朝下,但我不知道是否应该将所有内容重新定位为纵向或横向。

谢谢你!

4

3 回答 3

33

Apple 建议不要将设备方向用于视图布局。相反,每个视图控制器都有一个interfaceOrientation属性,并且UIApplication有一个statusBarOrientation属性,这两个属性都会返回当前的界面方向,适合视图布局。

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

于 2010-12-12T19:10:26.640 回答
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;
    }
  }

}
于 2015-09-16T16:27:49.427 回答
-1

I have the same problems with

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

This method generate notifications for 7 possible device positional states:

UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
UIDeviceOrientationUnknown

But I only need the first 4, well this is my approach to solve this problem (with code included): How to handle UIDeviceOrientation for manage views layouts

于 2011-06-18T00:41:11.753 回答