【问题标题】:How to determine device orientation when shouldAutorotate = NO and only portrait is allowed?当 shouldAutorotate = NO 并且只允许纵向时如何确定设备方向?
【发布时间】:2016-11-25 08:02:53
【问题描述】:

我正在做一个项目,我想根据设备的方向替换图标,而不允许 viewController 旋转。我有几个 viewController 允许旋转,而其他一些则在我的导航控制器中指定,不允许旋转。

这个实现完全按照我想要的方式工作。

我的导航控制器中的代码:

- (BOOL)shouldAutorotate
{
    id currentViewController = self.topViewController;

    if ([currentViewController isKindOfClass:[ChimpTabBarController class]]) {
        ChimpTabBarController *tabbarController = (ChimpTabBarController*)currentViewController;

        if (tabbarController.selectedIndex == 0){
            return NO;
        }
    } else if ([currentViewController isKindOfClass:[RotationViewController class]] || [currentViewController isKindOfClass:[ChimpBlackoutVC class]] || [currentViewController isKindOfClass:[EditPictureVC class]]) {
        return NO;
    }
    return YES;
}



-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    id currentViewController = self.topViewController;

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;


    if ([currentViewController isKindOfClass:[ChimpTabBarController class]]) {
        ChimpTabBarController *tabbarController = (ChimpTabBarController*)currentViewController;

        if (tabbarController.selectedIndex == 0){
            return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
        }
    } else if ([currentViewController isKindOfClass:[RotationViewController class]] || [currentViewController isKindOfClass:[ChimpBlackoutVC class]] || [currentViewController isKindOfClass:[EditPictureVC class]]) {
        return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
    }
    return UIInterfaceOrientationMaskAll;
}

有什么方法可以找出我的设备想要进行的旋转,但不允许?这样我就可以改变箭头,单词等的方向......

谢谢

【问题讨论】:

    标签: ios objective-c iphone rotation orientation


    【解决方案1】:

    我自己找到了方法,比预期的要早。

    对于您的应用程序,您可以使用:

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    

    但是当旋转被禁用时,您可以随时询问设备的方向:

    [[UIDevice currentDevice] orientation]
    

    然后在您的 viewcontroller 中添加一个观察者,用于查看设备何时改变方向:

    -->How to check Device Orientation Change From Portrait To Landscape and Vice-Versa in iPad

    希望这对未来的人们有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 2014-10-19
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多