shouldAutorotate未调用 - [不使用故事板]

时间:2016-10-24 13:40:04

标签: ios objective-c

我的UIViewController没有调用方法shouldAutorotate。
尝试了几种方式强制以纵向模式显示VC。

示例代码如下:

AppDelegate.m

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.navController = [[UINavigationController alloc] initWithRootViewController:[[VCLogin alloc] init]];
[self.window setRootViewController:self.navController];
[UIApplication sharedApplication].statusBarHidden = YES;
[self.window makeKeyAndVisible];

VCLogin.m

- (BOOL)shouldAutorotate
{
    return [self.navigationController.visibleViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationPortraitUpsideDown;
}

启用设备方向为“纵向”,“横向左侧”和“横向右侧”

任何想法都将受到赞赏 提前谢谢。

2 个答案:

答案 0 :(得分:0)

尝试添加以下代码:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
//or return UIInterfaceOrientationMaskPortrait , etc as your requirement
}

答案 1 :(得分:0)

我得到了解决方案。

添加了自定义UINavigationController类。

<强> navigationControllerViewController.h

#import <UIKit/UIKit.h>

@interface navigationControllerViewController : UINavigationController

@end

并覆盖以下方法

<强> navigationControllerViewController.m

- (BOOL)shouldAutorotate {
    return [self.visibleViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask )supportedInterfaceOrientations {
    return [self.visibleViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.visibleViewController preferredInterfaceOrientationForPresentation];
}

然后在里面分配了这个自定义导航控制器 AppDelegate.m文件

// Replace
self.navController = [[UINavigationController alloc] initWithRootViewController:[[VCMasterView alloc] init]];

// With
self.navController = [[navigationControllerViewController alloc] initWithRootViewController:[[VCMasterView alloc] init]];