【问题标题】:iOS (9 &10) App does not rotate upside downiOS (9 &10) 应用程序不会倒置旋转
【发布时间】:2017-07-11 07:50:43
【问题描述】:

我遇到了麻烦,如果我将手机倒置,我的应用就会倒置旋转。

我有一个包含两个场景的故事板文件: NavigationControllerScenes and MainScene / SettingsScene

在 info.plist 中,我选中了以纵向或倒置方向开始的框。

在我添加的应用委托中:

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    #else
    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    #endif
    {
      return UIInterfaceOrientationMaskPortrait;
    }

在设置视图控制器中:

        - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
    {
        return UIInterfaceOrientationPortraitUpsideDown;
    }


    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }

    - (BOOL)shouldAutorotate  // iOS 6 autorotation fix
    {
        return YES;
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
        - (NSUInteger)supportedInterfaceOrientations{
    #else
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    #endif
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    }

}

在主视图控制器中

        - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
    {
        return UIInterfaceOrientationPortraitUpsideDown;
    }


    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }

    - (BOOL)shouldAutorotate  // iOS 6 autorotation fix
    {
        return YES;
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }


    #if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
            - (NSUInteger)supportedInterfaceOrientations
    #else
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    #endif
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    }

可以旋转到横向和纵向,但不能颠倒纵向。 如果我取消选中 info.plist 中的纵向框并仅选中倒置框,则应用程序将在开始时崩溃。 所以我认为缺少一个设置...

我没有得到的是,我可以在只有一个场景的不同应用程序中成功使用这些代码 sn-ps - 所以我认为错误来自多个场景。 也许我需要创建一个导航控制器来覆盖一些方法。

还有一点: 这不起作用 - 应用程序执行语句但没有任何反应:

NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

在具有一个场景的应用程序中有效!

【问题讨论】:

标签: ios objective-c orientation landscape-portrait portrait


【解决方案1】:

终于我自己找到了答案:

如下创建一个CustomNavigationController类:

// .h file
#import <UIKit/UIKit.h>
@interface CustomNavigationController : UINavigationController
@end

// .m file
#import "CustomNavigationController.h"

@interface CustomNavigationController ()

@end

@implementation CustomNavigationController

- (BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}

@end

在情节提要中单击导航控制器场景。 在自定义类部分添加创建的类 as shown here

【讨论】:

    猜你喜欢
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    相关资源
    最近更新 更多