在UINavigationController中使用UITabBarController,处理Two-stage Rotation错误

写iOS应用时,经常需要将UITabBarController嵌入到一个根UINavigationController中,如果处理不好,我们会遇到这样的错误:

Two-stage rotation animation is deprecated. This application should use the smoother single-stage animation.

网上找了一下,StackOverFlow的这个答案说,不应该将UITabBarControllier嵌入到UINavigationController中作为rootViewController,但是,我们的确想要这样做,所以只好寻找其它办法。不过,至少我们可以确定的是,问题出在rootViewController同时包含UITabBarController和UINavigationController。

几经尝试,最后发现,在设置为window.rootViewController之前,先指定tabBarController.selectedIndex = 0,问题解决。

可以得出,出现上述错误,是因为XCode不知道你需要push哪个子viewController,在加载navigationController的时候,不知道要载入哪一个controller,于是无脑的将tabBarController的viewControllers都动画载入了。

完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];   UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];   UINavigationController *navVC = [storyBoard instantiateInitialViewController];  //MainViewController是UITabBarController的子类  MainViewController *rootVC = (MainViewController *)navVC.visibleViewController;  rootVC.delegate = self;  rootVC.selectedIndex = 0; //需要这样设置   self.window.rootViewController = navVC;   self.window.backgroundColor = [UIColor whiteColor];  [self.window makeKeyAndVisible];  return YES; } 

StoryBoard:

在UINavigationController中使用UITabBarController,处理Two-stage Rotation错误_第1张图片

运行效果:

在UINavigationController中使用UITabBarController,处理Two-stage Rotation错误_第2张图片

原文作者:  lslin
原文链接:  http://blog.lessfun.com/blog/2014/01/15/using-uitabbarcontroller-inside-uinavigationcontroller/
版权声明:自由转载-非商用-非衍生-保持署名 |  Creative Commons BY-NC-ND 3.0

你可能感兴趣的:(在UINavigationController中使用UITabBarController,处理Two-stage Rotation错误)