iPhone设备当前的方向在ViewDidLoad

请帮帮我。 我有方法:

-(void) getCurrentOrientation{ UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; if(orientation == UIInterfaceOrientationPortrait){ NSLog(@"portrait"); } else if(orientation == UIInterfaceOrientationLandscapeRight) { NSLog(@"LandscapeRight"); } else if(orientation == UIInterfaceOrientationLandscapeLeft) { NSLog(@"LandscapeLeft"); } } 

但是当我调用这个getCurrentOrientation抛出viewDidLoad

 - (void)viewDidLoad { [super viewDidLoad]; [self getCurrentOrientation]; } 

NSLog是空的。 怎么了 ? 我也试试

 - (void)viewDidLoad { [super viewDidLoad]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)) { NSLog(@"portrait"); } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) { NSLog(@"LandscapeRight"); } } 

但是那个varint也是空的。

我需要知道在哪个ORIENTATION用户启动APP!

请给我任何build议。

你的代码是绝对正确的,没有问题。

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

此声明仅适用于原始设备。

然而,你想检查模拟器,你可以检查

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { // portrait } else { // landscape } 

更新:

我已经在设备和模拟器中testing过你。 最初它只会在viewDidLoad中显示肖像,尽pipe你会保持横向的设备。 第一个控制器将考虑shouldAutoRotate方法。

你不应该依赖viewDidLoad的初始方向。 您应该首先依靠shouldAutorotate方法来确定方向。

尝试用下面的shouldAutorotate代替你的shouldAutorotateMethod

  - (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation { if(interfaceOrientation == UIInterfaceOrientationPortrait){ NSLog(@"portrait"); } else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) { NSLog(@"LandscapeRight"); } else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { NSLog(@"LandscapeLeft"); } return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown) } 

这可能会帮助你。

如果你只想检查应用程序的方向,然后使用下面的代码:

 - (void) viewDidLoad { [super viewDidLoad]; BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation); // now do whatever you need } 

要么

 -(void)viewDidLoad { if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) { //landscape view code } else { //portrait view code } } 

你的viewDidLoad是否被调用? 你有没有在那里放一个断点?

那么打印NSLog(@"%i", [[UIDevice currentDevice] orientation])呢?

该文档说,如果您没有调用beginGeneratingDeviceOrientationNotifications ,方向总是返回0。 也许在你尝试获得方向之前调用它是不够的。 尝试将调用移至application:didFinishLaunchingWithOptions:

但是,最好的方法是使用控制器提供的方向 – [UIViewController interfaceOrientation]和传递给shouldAutorotateToInterfaceOrientation的参数。

当应用程序第一次加载时,UIDevice.current.orientation无效。 但是UIApplication.shared.statusBarOrientation是。 一般来说,UIDevice.current.orientation是更好的检查方向的方法。 所以,这个方法将处理所有情况

 func isLandscape() -> Bool { return UIDevice.current.orientation.isValidInterfaceOrientation ? UIDevice.current.orientation.isLandscape : UIApplication.shared.statusBarOrientation.isLandscape }