获取设备当前方向(应用扩展)

18

如何在应用扩展中获取设备的当前方向,我尝试了以下两种方法但未成功。

  1. It always return UIDeviceOrientationUnknown

    [[UIDevice currentDevice] orientation]
    
  2. It shows red message that ‘sharedApplication’ is not available on iOS (App Extension)

    [[UIApplication sharedApplication] statusBarOrientation];
    
  3. I also add an observer but it not getting called.

    [[NSNotificationCenter defaultCenter] addObserver:self.view selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    
    
    
    - (void)notification_OrientationWillChange:(NSNotification*)n
    {
       UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];
    
        if (orientation == UIInterfaceOrientationLandscapeLeft)
           [self.textDocumentProxy insertText:@"Left"];
        if (orientation == UIInterfaceOrientationLandscapeRight)
           [self.textDocumentProxy insertText:@"Right"];
    }
    

那么现在如何获取当前设备方向呢?


你使用的是哪个iOS版本? - angerboy
Xcode6 beta4 与 iOS8。 - Anand Suthar
2
从 iOS 8 开始,苹果不再使用设备方向。请查看大小类。 - angerboy
我发现这个答案很有帮助: https://dev59.com/0l0b5IYBdhLWcg3wLOc2 - Sagi Mann
11个回答

-2

这段代码无法返回确切的UIDeviceOrientation,但你可以知道它是处于竖直或横向模式。

BOOL isLandScape = !(self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width*([[UIScreen mainScreen] bounds].size.width<[[UIScreen mainScreen] bounds].size.height))+([[UIScreen mainScreen] bounds].size.height*([[UIScreen mainScreen] bounds].size.width>[[UIScreen mainScreen] bounds].size.height)));

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