iOS 6 : supportedInterfaceOrientations is called but shouldAutorotate is not called

标签 ios iphone ios6 uinavigationcontroller uiinterfaceorientation

我知道这个问题之前在这里被问过 iOS 6 shouldAutorotate: is NOT being called .但我的情况有点不同。
最初,在应用程序启动时,我加载了一个 viewControllerself.window.rootViewController = self.viewController;并且当加载此 View 时,我正在使用自定义 tabBar View (从 UIView 继承),每个选项卡都有 5 个 UINavigationController。这个 viewController 类的代码是:

\\\ .h File

@protocol tabbarDelegate <NSObject>
-(void)touchBtnAtIndex:(NSInteger)index;
@end
@class tabbarView;
@interface ViewController : UIViewController <tabbarDelegate>
@property(nonatomic,strong) tabbarView *tabbar;
@property(nonatomic,strong) NSArray *arrayViewcontrollers;
@property(nonatomic,strong) UINavigationController * navigation1;
@property(nonatomic,strong) UINavigationController * navigation2;
@property(nonatomic,strong) UINavigationController * navigation3;
@property(nonatomic,strong) UINavigationController * navigation4;
@property(nonatomic,strong) UINavigationController * navigation5;
@end

和 m 文件是
- (void)viewDidLoad
{
[super viewDidLoad];

CGFloat orginHeight = self.view.frame.size.height- 60;
_tabbar = [[tabbarView alloc]initWithFrame:CGRectMake(0,  orginHeight, 320, 60)];
_tabbar.delegate = self;
[self.view addSubview:_tabbar];
_arrayViewcontrollers = [self getViewcontrollers];
[self touchBtnAtIndex:0];
}
-(void)touchBtnAtIndex:(NSInteger)index    \\This delegate method is called on every custom tabBar button clicked.
{
UIView* currentView = [self.view viewWithTag:SELECTED_VIEW_CONTROLLER_TAG];
[currentView removeFromSuperview];

UINavigationController *viewController = [_arrayViewcontrollers objectAtIndex:index];
viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;
viewController.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height- 50);

[self.view insertSubview:viewController.view belowSubview:_tabbar];
}
-(NSArray *)getViewcontrollers
{
FirstViewController *firstController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
ThirdViewController *thirdController = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];
ForthViewController *forthController = [[ForthViewController alloc]initWithNibName:@"ForthViewController" bundle:nil];
FifthViewController *fifthController = [[FifthViewController alloc]initWithNibName:@"FifthViewController" bundle:nil];

navigation1 = [[UINavigationController alloc] initWithRootViewController:firstController];
navigation2 = [[UINavigationController alloc] initWithRootViewController:secondController];
navigation3 = [[UINavigationController alloc] initWithRootViewController:thirdController];
navigation4 = [[UINavigationController alloc] initWithRootViewController:forthController];
navigation5 = [[UINavigationController alloc] initWithRootViewController:fifthController];

NSArray* tabBarItems = [[NSArray alloc] initWithObjects:navigation1,navigation2,navigation3,navigation4,navigation5, nil];
return tabBarItems;
}

我还实现了 UINavigationController 的旋转类别:
@implementation UINavigationController (autoRotation)
-(BOOL)shouldAutorotate {
return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations {
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

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

我的问题是只有 supportedInterfaceOrientations方法被调用,shouldAutorotate永远不会被调用。主窗口的 rootViewController 是 ViewController类,不是任何 UINavigationControllerUITabBarController .我做错了什么?请帮我。

最佳答案

如果将这些添加到 Root View Controller :

- (BOOL)shouldAutorotate
{
    NSLog(@"ViewController shouldAutorotate super=%d", [super shouldAutorotate]);
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    NSLog(@"ViewController supportedInterfaceOrientations");
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

您会看到系统不断地发送这两条消息。如果您愿意,您的代码可以查询事件的导航 Controller 以查看要返回的值。

此外,我不喜欢在 UINavigation Controller 上使用类别来覆盖这些 - 它的工作方式有点侥幸,并且导入其他一些执行相同操作的库可能会在以后导致意外后果。只需创建一个非常简单的子类并覆盖这两者。自 iOS4 以来,我一直在使用 UITabBarController 和 UINavigationController 这样做,并且从未遇到过问题。

关于iOS 6 : supportedInterfaceOrientations is called but shouldAutorotate is not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20120990/

相关文章:

iphone - 在 App Purchase 中不适用于 iPhone 5 设备

ios - 如何在运行时每 0.2 秒在屏幕上的不同位置重新创建屏幕上的相同节点?

ios - 如何在委托(delegate)中加载插页式广告并在另一个 View Controller 中显示?

ios - CallKit - "Touch to return to call"未删除

iphone - UIPickerView ios 问题 - 无法识别的选择器

iphone - 改变UIIMAGE+UIKIT+iphone的颜色

iphone - 在方法中返回自定义的 UIButton?

ios - 无法显示设置警报 iOS6 +

iphone - 在 UIViewController loadView 中使用什么尺寸?

ios - iOS 6 上的长期后台任务执行