在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都动画载入了。

完整代码如下:


- (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;

}

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