2012-12-12 73 views
18

我开发了一个iOS应用程序并在iOS6设备上进行了测试。在测试过程中,我意识到我的应用程序没有按预期响应方向更改。如何在iOS 6中以编程方式获取设备的当前方向?

这里是我的代码:

// Autorotation (iOS >= 6.0) 
- (BOOL) shouldAutorotate 
{ 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAll; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

准确地说,我想知道,什么方法都要求在iOS的取向的变化。

+0

“我的代码是不工作就定位”意味着? –

+0

其实我想知道当我旋转我的设备时调用哪个方法..? –

+0

它将调用shouldAutorotate方法并支持界面方向方法 –

回答

47

你可以试试这个,也许能帮助您:

How to change the device orientation programmatically in iOS 6

OR

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation] 
+9

请注意'属性总是返回0,除非通过调用beginGeneratingDeviceOrientationNotifications来启用方向通知。[[Docs]](http://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/uid/TP40006902-CH3-SW3) – DanSkeel

32

你可以试试这个,这解决您的问题。

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 

请参考下链接App extension Get device current orientation (App Extension)

+0

旋转设备时会调用以下方法。 shouldAutorotateToInterfaceOrientation,didRotateFromInterfaceOrientation&willRotateToInterfaceOrientation。 – Girish

+2

真的很糟糕的主意。不要挂在那个属性上。 – DanSkeel

+1

@DanSkeel它怎么不好?它工作正常。 – Girish

2

按照上UIDevice的文档。

你需要调用

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

然后在每次方向改变时,您将收到一个UIDeviceOrientationDidChangeNotification。

+0

亲爱的我问iOS 6 .... –

+0

这适用于任何iOS自2.0 –

2

This tutorial给出了一个很好的和简单的概述如何处理设备旋转使用UIDeviceOrientationDidChangeNotification。它应该可以帮助您了解如何在设备方向更改时通知UIDeviceOrientationDidChangeNotification

5

/*您需要在viewDidLoad中声明这些代码或viewWillAppear中*/

- (void)viewDidLoad 

    { 

    [super viewDidLoad]; 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil]; 
} 

/*现在我们的设备将提供一个通知,每当我们改变设备的方向,因此,您可以使用当前方向控制您的代码或程序*/

- (void)deviceOrientationDidChange:(NSNotification *)notification 

{ 

//Obtaining the current device orientation 

/* Where self.m_CurrentOrientation is member variable in my class of type UIDeviceOrientation */ 

self.m_CurrentOrientation = [[UIDevice currentDevice] orientation]; 

    // Do your Code using the current Orienation 

} 
+0

这不适用于我在iOS9.2 – Sanju

+0

如何检查肖像和通过使用self.m_CurrentOrientation景观? – Sanju

+0

if(UIDeviceOrientationIsLandscape(self.m_CurrentOrientation) { //横向取向代码 } – Vicky

0

I在您的ViewController中,您可以使用didRotateFromInterfaceOrientation:方法来检测设备何时旋转,然后在每个方向上执行任何您需要的操作。

例子:

#pragma mark - Rotation 

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    switch (orientation) { 
     case 1: 
     case 2: 
      NSLog(@"portrait"); 
      // your code for portrait... 
      break; 

     case 3: 
     case 4: 
      NSLog(@"landscape"); 
      // your code for landscape... 
      break; 
     default: 
      NSLog(@"other"); 
      // your code for face down or face up... 
      break; 
    } 
} 
2

也许愚蠢的,但它是工作(仅在视图控制器):

if (self.view.frame.size.width > self.view.frame.size.height) { 
    NSLog(@"Hello Landscape"); 
} 
相关问题