首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 6:调用supportedInterfaceOrientations,但不调用shouldAutorotate

iOS 6:调用supportedInterfaceOrientations,但不调用shouldAutorotate
EN

Stack Overflow用户
提问于 2013-11-21 12:26:31
回答 1查看 2.7K关注 0票数 1

我知道以前有人问过这个问题,iOS 6 shouldAutorotate: is NOT being called。但我的情况有点不同。

最初,在应用程序启动时,我加载了一个viewController,类似于

self.window.rootViewController = self.viewController;

当加载此视图时,我使用的是自定义tabBar视图(从UIView继承),该视图每个选项卡都有5个UINavigationController。这个viewController类的代码是:

代码语言:javascript
复制
\\\ .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文件是

代码语言:javascript
复制
- (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的轮调类别如下:

代码语言:javascript
复制
@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。我做错什么了?请帮帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-21 15:24:31

如果将这些添加到根视图控制器中:

代码语言:javascript
复制
- (BOOL)shouldAutorotate
{
    NSLog(@"ViewController shouldAutorotate super=%d", [super shouldAutorotate]);
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    NSLog(@"ViewController supportedInterfaceOrientations");
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

您将看到系统不断地发送这两条消息。您的代码可以查询active,以查看如果您愿意返回哪些值。

而且,我不喜欢在UINavigation控制器上使用一个类别来覆盖这些类--它的工作方式有点侥幸,并且导入其他类似的库可能会在稍后造成意想不到的后果。只需创建一个非常简单的子类,然后遍历这两个子类。从UITabBarController和UINavigationController开始,我就一直这样做,而且从来没有遇到过任何问题。

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

https://stackoverflow.com/questions/20120990

复制
相关文章

相似问题

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