用动画隐藏导航控制器和标签栏控制器
Posted
技术标签:
【中文标题】用动画隐藏导航控制器和标签栏控制器【英文标题】:Hide both Navigation Controller and Tab Bar Controller with animation 【发布时间】:2012-10-25 15:16:15 【问题描述】:我试图在触摸按钮时同时隐藏 UITabBarController 和 UINavigationController。我在这里找到了一个非常好的代码 sn-p How to hide uitabbarcontroller,但是在尝试隐藏 UINavigationController 和 tabbarcontroller 并为其设置动画时遇到问题。当他们使用self.tabBarController.tabBar.hidden = YES
隐藏标签栏时,我还在互联网上找到了很多示例,但这仅隐藏了按钮项而不是底部的黑条。
在玩了很多之后,我可以正确地制作两个动画,因为我认为它与导航控制器的隐藏有关,这使得整个窗口的大小可以即时改变。
-(IBAction)touchImage:(id)sender
if (isImageFullScreen)
isImageFullScreen = NO;
[self.navigationController setNavigationBarHidden:NO animated:YES];
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionCurveLinear
animations:^
hotelImageButton.frame = CGRectMake(0,20,320,92);
[self showTabBar:self.tabBarController];
completion:^(BOOL finished)
];
else
isImageFullScreen = YES;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionCurveLinear
animations:^
hotelImageButton.frame = CGRectMake(0,0,320,480);
[self hideTabBar:self.tabBarController];
completion:^(BOOL finished)
];
hideTabBar 和 showTabBar 方法是我上面链接的另一篇文章中的方法。
我也尝试了一些其他的组合,但我不能让它看起来很好。有什么想法吗?
提前致谢。
【问题讨论】:
【参考方案1】:我现在尝试了该代码,我发现 UITabBar 显示动画不能顺利进行。 通过将显示动画的标签栏的持续时间调整为更低,我设法使其更流畅。
[UIView setAnimationDuration:0.2];
希望这行得通。
编辑: 请尝试此代码,它会在 1 个动画事务中将父视图的大小调整为更大,从而仅隐藏条形并显示内容。
- (IBAction)TestButton1:(UIButton *)sender
if(!isAnimating)
if(isTabBarAndNavBarHidden)
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^
isAnimating=YES;
CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;
[self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, 0, self.tabBarController.view.frame.size.width, screen_height)];
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, statusBar_height, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
completion:^(BOOL finished)
isTabBarAndNavBarHidden=NO;
isAnimating=NO;
];
else
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^
isAnimating=YES;
CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;
[self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, statusBar_height-self.navigationController.navigationBar.frame.size.height, self.tabBarController.view.frame.size.width, screen_height+self.navigationController.navigationBar.frame.size.height+self.tabBarController.tabBar.frame.size.height-statusBar_height)];
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
completion:^(BOOL finished)
isTabBarAndNavBarHidden=YES;
isAnimating=NO;
];
【讨论】:
但除此之外,你不是得到一个奇怪的动画吗?酒吧移动奇怪。就像全部到底部然后出现一半然后动画。 不,它按预期工作。如果您只隐藏标签栏或导航栏,它对您有用吗? 如果我只隐藏标签栏就可以了。问题是当试图隐藏两者时。 试试我附在答案中的代码,告诉我它是否适合你。 还有一些最后的润色,但按预期工作了一点。感谢您的帮助。【参考方案2】:此代码适用于 iPhone 4/4S。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
if (self.lastContentOffset > scrollView.contentOffset.y)
NSLog(@"Scrolling up");
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^
[self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
completion:
^(BOOL finished)
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^
completion:^(BOOL finished)
//
];
];
else if (self.lastContentOffset < scrollView.contentOffset.y)
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^
[self.navigationController.navigationBar setFrame:CGRectMake(0, -60, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[self.tabBarController.tabBar setFrame:CGRectMake(0, 480, 320, 50)];
completion:
^(BOOL finished)
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^
completion:^(BOOL finished)
];
];
NSLog(@"Scrolling Down");
self.lastContentOffset = scrollView.contentOffset.y;
- (void)viewDidLoad
[super viewDidLoad];
[self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
// Do any additional setup after loading the view.
【讨论】:
以上是关于用动画隐藏导航控制器和标签栏控制器的主要内容,如果未能解决你的问题,请参考以下文章
关于iOS导航控制器隐藏和显示会出现返回键失效,导航栏标题动画异常