【问题标题】:iOS 6: supportedInterfaceOrientations is called but shouldAutorotate is not callediOS 6:supportedInterfaceOrientations 被调用,但 shouldAutorotate 未被调用
【发布时间】:2013-12-05 22:10:57
【问题描述】:

我知道在iOS 6 shouldAutorotate: is NOT being called 之前有人问过这个问题。但我的情况有点不同。
最初,在应用程序启动时,我加载了一个 viewController,例如
self.window.rootViewController = self.viewController;
并且当加载此视图时,我正在使用自定义 tabBar 视图(从 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。我做错了什么?请帮帮我。

【问题讨论】:

  • 这里有一些(可能)有用的讨论:stackoverflow.com/q/12775265/653513
  • @rokjarc 我在开头提到我已经看到了这个线程,我的问题有点不同。我的窗口的rootViewControllerviewController 类,而不是任何UINavigationControllerUITabBarController。那里对我没有任何帮助。
  • 在 ios7 中的行为是否相同?如果是这样,只需将一个小型 dem 项目放在一个选项卡上,然后将其上传到公共场所。
  • @DavidH 好的。将在一小时内发送给您。

标签: ios iphone ios6 uinavigationcontroller uiinterfaceorientation


【解决方案1】:

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

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

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

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

【讨论】:

  • 它按我的预期工作。你真的是一个救生员(以编程方式)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 2019-12-23
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多