在ViewDidLoad中获取iPhone设备当前方向

9

请帮帮我。我有以下方法:

-(void) getCurrentOrientation{

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

    if(orientation == UIInterfaceOrientationPortrait){
        NSLog(@"portrait");
    } else if(orientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"LandscapeRight");
    } else if(orientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"LandscapeLeft");
    }   
}

但是当我在viewDidLoad中调用getCurrentOrientation时,会出现以下问题。
- (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也是空的。

我需要知道用户启动应用程序的方向!

请给我任何建议。

6个回答

15

你的代码完全正确,没有任何问题。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

这个语句只适用于原始设备。

不过,如果你想在模拟器上进行检查,可以按照以下步骤:

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

更新:

我已经为您在设备和模拟器上进行了测试。最初,即使您将设备保持在横向模式下,它也只会在viewDidLoad方法中显示纵向模式。第一个控制器将查看shouldAutoRotate方法。

您不应该依赖于 viewDidLoad 方法来确定初始方向。您应该最初依赖于shouldAutorotate方法来获取确切的方向。


谢谢。但是当我在横屏模式下在模拟器上运行时,NSlog输出PORTRAIT。所以我不确定它是否能够在真实设备上正确工作。 - user1135750
我在项目-目标-摘要-支持的设备方向中只选择了LANDSCAPE。但是当我在模拟器中运行应用程序时,使用您的函数Nslog输出肖像。为什么我不应该依赖?我只需要知道用户运行应用程序的方向。 - user1135750
1
正如我告诉你的那样,如果你支持竖屏/横屏模式,用户在初始时以横屏模式运行应用程序,它将加载竖屏模式。在执行 shouldAutorotate 函数后,它会知道当前是横屏模式,你应该只在其中放置你的代码。希望这清楚了概念。如果仍然有疑问,请留下评论。 - Janak Nirmal
谢谢!我认为那是解决方案!再次感谢您! - user1135750

8
当应用程序第一次加载时,UIDevice.current.orientation无效。但UIApplication.shared.statusBarOrientation是有效的。总的来说,UIDevice.current.orientation是检查方向的更好方式。因此,这种方法将处理所有情况。
var isLandscape: Bool {
    return UIDevice.current.orientation.isValidInterfaceOrientation 
        ? UIDevice.current.orientation.isLandscape 
        : UIApplication.shared.statusBarOrientation.isLandscape
}

1
这个,我简直不敢相信这没有被标记为正确答案,非常感谢你 :) - Lennart P.

2
尝试使用以下 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)
  }

这可能会帮助你解决问题。

请告诉我,为什么?我对 shouldAutorotateToInterfaceOrientation 没有任何问题。我的问题出在 viewDidLoad 上。 - user1135750

2

如果您只想检查应用的方向,请使用以下代码:

- (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
    }
}

2
在Swift 3或Swift 4中,您可以在viewDidLoad()中使用以下代码。
let orientation = UIApplication.shared.statusBarOrientation
 if orientation == .portrait {
        // portrait   
 } else if orientation == .landscapeRight || orientation == 
.landscapeLeft{
         // landscape     
 }

1

你的 viewDidLoad 被调用了吗?你在那里设置了断点吗?

试试只打印 NSLog(@"%i", [[UIDevice currentDevice] orientation]),看看会发生什么?

文档说,如果你没有调用 beginGeneratingDeviceOrientationNotifications,方向始终返回 0。也许在尝试获取方向之前调用它不够。试着将调用移动到 application:didFinishLaunchingWithOptions:

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


谢谢您的回复。是的,方向始终返回0,并且我放置了(如我上面所写)beginGeneratingDeviceOrientationNotifications。但方向仍然返回0。 - user1135750
这就是为什么我建议你尝试将它移动到 [UIApplicationDelegate application:didFinishLaunchingWithOptions:] 的原因。 - Sulthan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接