今天做项目的时候遇到要处理在页面跳转的时候隐藏tabbar,找了很多方法都没有实现,要么是用self.tabBarController.tabBar.hidden = YES出现黑边,要么就是使用self.hidesBottomBarWhenPushed = YES出现B也不显示tabbar的BUG,最后在app4code上看到了一个方法可以解决我的这个问题。
我的问题是rootViewController:A,subview:B,B可以push到C,D两个viewcontroller。B显示tabbar,C、D不显示tabbar。
代码简单,大家直接自己看就OK了。
在C和D中写如下方法:
-(void) viewDidAppear:(BOOL)animated
{
self.hidesBottomBarWhenPushed = YES;
}
-(void)viewWillAppear:(BOOL)animated
{
[self hideTabBar];
}
- (void)hideTabBar {
if (self.tabBarController.tabBar.hidden == YES) {
return;
}
UIView *contentView;
if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
contentView = [self.tabBarController.view.subviews objectAtIndex:1];
else
contentView = [self.tabBarController.view.subviews objectAtIndex:0];
contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
self.tabBarController.tabBar.hidden = YES;
}
在B中写如下方法
-(void)viewWillAppear:(BOOL)animated
{
[self hideTabBar];
}
- (void)hideTabBar {
if (self.tabBarController.tabBar.hidden != YES)
{
return;
}
self.tabBarController.tabBar.hidden = NO;
}
就可以实现在C和D界面隐藏tabbar,在B页面显示tabbar并且不会出现黑边情况。