IOS7 中典型的tabarcontroller+navigationbarcontroller+viewcontroller组合 的一个问题解决

分割线下面的东东严重错误了,其实只要在 pushViewController之前进行.hidesBottomBarWhenPushed = YES; 就可以!(被pushd的vc不需要调用hidetab...)

原本很简单的问题,搞了复杂的搞了半天,回想下原因:知识有盲区,而且查了资料后,就顺着资料的思路在查问题...  

以后遇到问题要先查stackoverflow,想好关键词就可以 大部分都可以找到解决办法。


不过,这几天的纠结也不是全无用,起码用reveal熟悉了些view结构等等。


======================================================================================================================================================分割线=====================================================================

整合原来的项目代码到IOS7下,原代码根本不是使用标题的结构做的(显示出来的navigationbar只是一个bar),并且视图间都用的是presentViewController,而非pushViewController,这样很不规范。

于是修改了结构,一般而言大多是一个navigationbar里面的第一个viewcontroller显示tabar,后续再push的话,都不显示了(参考qq等等),于是使用网上查到的隐藏tabar的方法。

//最后用
- (void)hideTabBar
{
    if (self.tabBarController.tabBar.hidden == YES)
    {
        return;
    }
    UIView *contentView = nil;
    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];
    
    if(contentView.frame.size.height < ScreenHeight)
    {
        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;
}


/*
 虽然解决了隐藏系统TabBar,显示自定义的TabBar并且视图不会错位的问题。但是不明白原理。希望高手解答一下。或是有什么另外的方法。
 
 PS:用以上的方法隐藏系统TabBar,在需要显示系统的TabBar的时候要另外设置
 */
- (void)showTabBar
{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        return;
    }
    UIView *contentView = nil;
    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 = NO;
}
在每个viewcontroller中都有如下代码(当然,可以写到一个类中,然后继承)

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if([self.navigationController.viewControllers count] == 2)
        [self hideTabBar];//self.navigationController.tabBarController.tabBar.hidden = YES;
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.hidesBottomBarWhenPushed = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if([self.navigationController.viewControllers count] == 1)
        [self showTabBar];//self.navigationController.tabBarController.tabBar.hidden = NO;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
//    self.hidesBottomBarWhenPushed = NO;
}

更重要的是,每个navigationcontroller的rootviewcontroller也要有调用:self.hidesBottomBarWhenPushed = YES;比如:

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    
    self.hidesBottomBarWhenPushed = YES;//important!!
    ....
}

上面的这个很重要,如果不设置,会出现某些问题:比如 当push一个viewcontroller,而这个viewcontroller的view上面有UIsearchbar及UIsearchdisplaycontroller,就会出现点击searchbar后返回,发现此时视图变短了49单位的奇怪问题,而且如果后续是present一个viewcontroller的话,当dismiss后,也会有49单位变短的现象。


上面写得很乱,有空的时候,写一个框架代码吧。


上面很可能有隐藏的问题,如果有同学知道,请指点下吧,谢谢。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值