首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS (9 &10)应用程序不会倒转

iOS (9 &10)应用程序不会倒转
EN

Stack Overflow用户
提问于 2017-07-11 07:50:43
回答 1查看 425关注 0票数 0

我有麻烦,迫使我的应用程序旋转倒转,如果我是拿着手机倒转。

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

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

在应用程序委托中,我添加了:

代码语言:javascript
复制
#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;
    }

在“设置”视图控制器中:

代码语言:javascript
复制
        - (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;
    }

}

在主视图控制器中

代码语言:javascript
复制
        - (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中的肖像框,只选中倒挂框,该应用程序将在一开始崩溃。所以我觉得没有设置..。

我不明白的是,我可以在一个不同的应用程序中成功地使用这些代码片段,只有一个场景--所以我认为错误来自多个场景。也许我需要创建一个覆盖某些方法的navigationController。

还有一件事:这是行不通的--应用程序执行语句,但什么也没有发生:

代码语言:javascript
复制
NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

在这个只有一个场景的应用程序中,它是有效的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-25 08:12:53

最后,我自己找到了答案:

创建一个CustomNavigationController类如下所示:

代码语言:javascript
复制
// .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

在故事板中,单击导航控制器场景。在自定义类部分如图所示中添加创建的类

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45028500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档