Iphone隱藏和顯示TabBar的方法

原文地址:http://www.gowhich.com/blog/172web

關於顯示和隱藏TabBar的方法,本身開始不是很懂,查找了不少的資料,在http://blog.csdn.net/riveram/article/details/7345577app

這裏面,有這樣的描述,裏面是給了兩個方法:ide

隱藏tabbar的方法:網站


- (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;
    
}
顯示tabbar的方法:



- (void)showTabBar

{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        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 = NO;
    
}
而後直接調用就行了。


另外在cocoachina的網站上,也看到了一個方法,不過只是說是隱藏的方法。spa

代碼以下:.net


隱藏Tabbar

- (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;
    //    [UIView commitAnimations];    
} 
可是,有通過資料的查找,有個比較簡單的方法


我在A視圖的代碼可參考以下:orm


-(void) viewWillAppear:(BOOL)animated
{
    
    
    //添加右側的item
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.titleLabel.font = [UIFont fontWithName:@"Avenir-Book" size:14.0];
    _rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _rightButton.contentEdgeInsets = UIEdgeInsetsMake(0.0, 2.0, 0.0, 0.0);
    [_rightButton setFrame:CGRectMake(0.0, 0.0, 60.0, 30.0)];
    NSString *arrowPath = [[NSBundle mainBundle] pathForResource:@"littleArrow" ofType:@"png"];

    [_rightButton setBackgroundImage:[UIImage p_w_picpathWithContentsOfFile:arrowPath] forState:UIControlStateNormal];
    [_rightButton addTarget:self
                   action:@selector(personRoleSelect:)
         forControlEvents:UIControlEventTouchUpInside];
    
    if([_role isEqualToString:@""] || (_role == nil))
    {
        [_rightButton setTitle:@"所有" forState:UIControlStateNormal];
    }
    else
    {
        [_rightButton setTitle:[NSString stringWithFormat:@"%@",_role] forState:UIControlStateNormal];
    }
    
    
    
    [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UIBarButtonItem *rightItemBar = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
    self.navigationItem.rightBarButtonItem = rightItemBar;
    
    self.hidesBottomBarWhenPushed = YES;
    
    UIBarButtonItem *returnItem = [[UIBarButtonItem alloc] init];
    returnItem.title = @"返回";
    self.navigationItem.backBarButtonItem = returnItem;
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.hidesBottomBarWhenPushed = NO;
    [_dropDown hideDropDown:_rightButton];
}


主要是這句:blog

self.hidesBottomBarWhenPushed = YES;get

string

self.hidesBottomBarWhenPushed = NO;

相關文章
相關標籤/搜索