【问题标题】:shouldAutorotate Not called - [Not using storyboard]shouldAutorotate 未调用 - [不使用情节提要]
【发布时间】:2016-10-30 16:40:29
【问题描述】:

我的 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;
}

并将设备方向启用为“纵向”、“横向左侧”和“横向右侧”

任何想法将不胜感激。
提前致谢。

【问题讨论】:

  • 您需要保护特定的 vc 使其处于纵向模式?
  • 是的。我正在使用 iOS 10 sdk

标签: ios objective-c


【解决方案1】:

尝试添加以下代码:

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

【讨论】:

  • 其实我是想在整个应用中只维护一个纵向模式的vc。
  • 在情节提要中使用 sizeclass 制作视图控制器:紧凑的宽度和常规的高度
  • 不幸的是我的项目不在 storyboard 中。它只保留 xib 文件。
  • 然后在该 xib 中使用自动布局和 sizeclasses 并为其使用紧凑宽度和常规高度
【解决方案2】:

我找到了解决方案。

添加了一个自定义 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]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 2012-03-24
    相关资源
    最近更新 更多