UITabBarController 中的 UINavigationController 奇怪的弹出动画
Posted
技术标签:
【中文标题】UITabBarController 中的 UINavigationController 奇怪的弹出动画【英文标题】:UINavigationController in UITabBarController strange pop animation 【发布时间】:2013-01-09 17:18:53 【问题描述】:当我创建四个导航控制器并将它们添加到 UITabBar 时:
// Create the root view controllers for the tab bar
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
thirdViewController = [[ThirdViewController alloc] init];
fourthViewController = [[FourthViewController alloc] init];
// Configure the tab bar
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
tabBarController.viewControllers = @[
[[[UINavigationController alloc] initWithRootViewController:firstViewController] autorelease],
[[[UINavigationController alloc] initWithRootViewController:secondViewController] autorelease],
[[[UINavigationController alloc] initWithRootViewController:thirdViewController] autorelease],
[[[UINavigationController alloc] initWithRootViewController:fourthViewController] autorelease]
];
tabBarController.selectedIndex = 1;
self.window.rootViewController = tabBarController;
我遇到了一个问题,即哪个 UINavigationController 在启动时首次可见(在本例中为索引 1)有一个奇怪的“弹出”动画。导航栏中的标题等动画正常,但导航控制器的内容更改时没有动画。
选择一个不同的选项卡,然后返回到原来的选项卡可以解决问题。
另外,如果我将self.window.rootViewController
设置为[[[UINavigationController alloc] initWithRootViewController:secondViewController] autorelease]
以便将标签栏排除在等式之外,导航控制器就可以正常工作。
有什么想法吗?
【问题讨论】:
【参考方案1】:我遇到了同样的问题,我找到的唯一解决方案就是这个小技巧:
把它放在第一个 UITabBarController 视图控制器上的“(void)viewWillAppear”方法中。
UITabBarController * controller = self.tabBarController;
//Create this BOOL variable on your class and set it to YES on viewDidLoad
if(firstTimeViewLoaded)
// Simulate a click on other tab item then switch back instantly.
[controller setSelectedViewController:controller.viewControllers[1]];
[controller setSelectedViewController:controller.viewControllers[0]];
firstTimeViewLoaded = NO;
【讨论】:
【参考方案2】:我遇到了同样的问题并找到了解决方案,所以对于那些在谷歌搜索时偶然发现此页面的人:
来自Pop animation is not working in first UINavigationController of UITabbarController:
您可能忘记在-(void)viewDidAppear:(BOOL)animated
方法中写入[super viewDidAppear:animated];
:
-(void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
这应该可以解决它。
【讨论】:
【参考方案3】: // Create the root view controllers for the tab bar
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
thirdViewController = [[ThirdViewController alloc] init];
fourthViewController = [[FourthViewController alloc] init];
UINavigationController *myNavigationController;
UITabBarController *myTabBarController = [[UITabBarController alloc] init];
NSMutableArray *myTabs = [[NSMutableArray alloc] init];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[myTabs addObject:myNavigationController];
//Release
[myNavigationController release];
//Second view
myNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[myTabs addObject:myNavigationController];
[myNavigationController release];
//And so on with the third and fourth view controller
//...
[tabBarController setViewControllers:myTabs];
//Add the tab bar controller view to the main view
[self.window addSubview:tabBarController.view];
【讨论】:
以上是关于UITabBarController 中的 UINavigationController 奇怪的弹出动画的主要内容,如果未能解决你的问题,请参考以下文章
UITabBarController 问题中的 UINavigationController
UITabBarController 中的 UINavigationController 没有完全包裹 UIViewController