ViewDidLoad中的iphone设备当前方向

时间:2012-03-19 12:38:44

标签: iphone ios

请帮帮我。我有方法:

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

请给我任何建议。

6 个答案:

答案 0 :(得分:15)

您的代码绝对正确且没有问题。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

但是,您想要检查模拟器,您可以检查

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

<强>更新

我已经在设备和模拟器中测试了你。最初它只会在viewDidLoad中显示肖像,但您会将设备保持在横向状态。 First Controller将研究shouldAutoRotate方法。

您不应该viewDidLoad依赖作为初始方向。您最初应该依赖于shouldAutorotate方法来确切定位。

答案 1 :(得分:4)

首次加载应用时,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
}

答案 2 :(得分: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)
  }

这可能会帮助你。

答案 3 :(得分: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
    }
}

答案 4 :(得分:1)

你的viewDidLoad被叫了吗?你有断点吗?

如何打印NSLog(@"%i", [[UIDevice currentDevice] orientation])

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

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

答案 5 :(得分:0)

在swift 3或swift 4中。您可以在viewDidLoad()..

中使用此代码
`let orientation = UIApplication.shared.statusBarOrientation
 if orientation == .portrait {
        // portrait   
 } else if orientation == .landscapeRight || orientation == 
.landscapeLeft{
         // landscape     
 }`
相关问题