由于手机界面比较小,有时候由于业务的需要,需要隐藏那个导航用的tabbar,方法有下面几个方法
方法一
YourAppDelegate *app = (YourAppDelegate *)[UIApplication sharedApplication].delegate;
NSArray *views = [app.tabBarController.view subviews];
for(id v in views){
if([v isKindOfClass:[UITabBar class]]){
[(UITabBar *)v setHidden:YES];
}
至于是隐藏还是显示根据自己的业务需求,在ViewController的不同的生命周期里执行,
推荐在下面2个方法里隐藏和显示
- (void)viewWillAppear: (BOOL)animated
- (void)viewWillDisappear: (BOOL)animated
方法二
[viewController setHidesBottomBarWhenPushed:YES];
这个方法在viewcontroller push navigation之前执行,也还比较方便
补充一:navigationController +tabBarController的结构的话,在push到下一级的childController的时候用
childController.hidesBottomBarWhenPushed = YES;
detailViewController *detailView=[[detailViewController alloc] initwithDic:dic];
detailView.hidesBottomBarWhenPushed = YES;
detailView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:detailView animated:YES];
补充二:隐藏tabbar方式:
在pushViewController之前调用
[self setHidesBottomBarWhenPushed:YES];同时在viewWillDisappear调用:
- (void)viewWillDisappear:(BOOL)animated {
[self setHidesBottomBarWhenPushed:NO];
[super viewDidDisappear:animated];
}
方法三
- (void) hideTabBar:(BOOL) hidden
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0];
for(UIView *view in self.tabBarController.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
if (hidden) {
[view setFrame:CGRectMake(view.frame.origin.x, 320, view.frame.size.width, view.frame.size.height)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, 320-49, view.frame.size.width, view.frame.size.height)];
}
}
else
{
if (hidden) {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 320)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 320-49)];
}
}
}
[UIView commitAnimations];
}
这个方法,没有试过,如果把frame的位置调整好,应该是可以解决的但是在解决横屏和竖屏的时候,可能要花点功夫。
- (void)makeTabBarHidden:(BOOL)hide
{
if ( [self.tabBarController.view.subviews count] < 2 )
{
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];
}
// [UIView beginAnimations:@"TabbarHide" context:nil];
if ( hide )
{
contentView.frame = self.tabBarController.view.bounds;
}
else
{
contentView.frame = CGRectMake(self.tabBarController.view.bounds.origin.x,
self.tabBarController.view.bounds.origin.y,
self.tabBarController.view.bounds.size.width,
self.tabBarController.view.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
}
self.tabBarController.tabBar.hidden = hide;
}