首页 > 代码库 > 两种方式自定义标签工具栏

两种方式自定义标签工具栏

在AppDelegate.m中设置根视图控制器

MainTabbarController.h

@interface MainTabbarController : UITabBarController
{
    UIImageView *_selectedImg;
    UIView *_tabbarView;
}

- (void)showtabbar:(BOOL)show;

MainTabbarController.m

#import "MainTabbarController.h"
#import "BaseNavgationController.h"
#import "HomeViewController.h"
#import "SquleViewController.h"
#import "SearchViewController.h"
#import "CommentViewController.h"
#import "MessageViewController.h"

@interface MainTabbarController ()

@end

@implementation MainTabbarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //创建三级控制器
    [self _initViewCtrl];
    
    //自定义标签工具栏
//    [self _initTabbarView];
    [self _newInitTabarView];
}


//创建三级控制器
- (void)_initViewCtrl {

    //创建一级视图控制器
    HomeViewController *homeCtrl = [[HomeViewController alloc] init];
    SquleViewController *squleCtrl =[[SquleViewController alloc] init];
    SearchViewController *searchCtrl =[[SearchViewController alloc] init];
    CommentViewController *commentCtrl = [[CommentViewController alloc] init];
    MessageViewController *messageCtrl = [[MessageViewController alloc] init];
    
    NSArray *viewCtrls = @[homeCtrl,squleCtrl,searchCtrl,commentCtrl,messageCtrl];
    
    //创建二级导航控制器
    /*
    BaseNavgationController *navCtrl1 = [[BaseNavgationController alloc] initWithRootViewController:homeCtrl];
    BaseNavgationController *navCtrl2 = [[BaseNavgationController alloc] initWithRootViewController:squleCtrl];
    BaseNavgationController *navCtrl3 = [[BaseNavgationController alloc] initWithRootViewController:searchCtrl];
    BaseNavgationController *navCtrl4 = [[BaseNavgationController alloc] initWithRootViewController:commentCtrl];
    BaseNavgationController *navCtrl5 = [[BaseNavgationController alloc] initWithRootViewController:messageCtrl];
     */
    //使用循环创建
    
    NSMutableArray *navCtrls = [[NSMutableArray alloc] initWithCapacity:5];
    
    for (int i=0; i<5; i++) {
        UIViewController *viewCtrl = viewCtrls[i];
        BaseNavgationController *navCtrl = [[BaseNavgationController alloc] initWithRootViewController:viewCtrl];
        //将控制器添加到数组中
        [navCtrls addObject:navCtrl];
    }
    
    //将导航控制器存放到数组中
//    NSArray *navCtrls = @[navCtrl1,navCtrl2,navCtrl3,navCtrl4,navCtrl5];
    
    //创建三级标签控制器
    self.viewControllers = navCtrls;
    
}

//自定义工具栏
- (void)_initTabbarView {

    //1.隐藏默认生成的工具栏
    self.tabBar.hidden = YES;
    
    //2.创建工具栏的视图
    _tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 480-49, 320, 49)];
    _tabbarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navbg"]];
    [self.view addSubview:_tabbarView];
    
    //3.添加子视图按钮
    for (int i=0; i<5; i++) {
        //设置按钮的图片的名字
        NSString *name = [NSString stringWithFormat:@"%d",i+1];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        button.tag = i;
        button.frame = CGRectMake((64-42)/2+64*i, (49-44)/2, 42, 44);
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_tabbarView addSubview:button];
    }
    
    //4.创建选中图片
    _selectedImg = [[UIImageView alloc] initWithFrame:CGRectMake((64-53)/2, (49-45)/2, 53, 45)];
    _selectedImg.image = [UIImage imageNamed:@"选中"];
    [_tabbarView addSubview:_selectedImg];
    
}

//2.方式二:保留tabbar的自定义工具栏
- (void)_newInitTabarView {

<strong><span style="color:#cc0000;">    //1.移除工具栏上所有的按钮</span></strong>
    //取得所欲的子视图
    NSArray *subViews = [self.tabBar subviews];
//    NSLog(@"-----subViews:%@",subViews);
    //移除UITabBarButton的对象
    for (UIView *view in subViews) {
        //将UITabBarButton类型的对象移除
        
        //将UITabBarButton转换成类名
        Class class = NSClassFromString(@"UITabBarButton");
        
        //移除视图
        if ([view isKindOfClass:class]) {
            [view removeFromSuperview];
        }
    }
    
    //2.设置tabbarView的背景颜色
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"navbg"]];
    
    //3.添加子视图按钮
    for (int i=0; i<5; i++) {
        //设置按钮的图片的名字
        NSString *name = [NSString stringWithFormat:@"%d",i+1];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        button.tag = i;
        button.frame = CGRectMake((64-42)/2+64*i, (49-44)/2, 42, 44);
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];
    }
    
    //4.创建选中图片
    _selectedImg = [[UIImageView alloc] initWithFrame:CGRectMake((64-53)/2, (49-45)/2, 53, 45)];
    _selectedImg.image = [UIImage imageNamed:@"选中"];
    [self.tabBar addSubview:_selectedImg];
}

- (void)buttonAction:(UIButton *)button {

    //切换试图
    self.selectedIndex = button.tag;
    
    [UIView beginAnimations:NULL context:nil];
    [UIView setAnimationDuration:.3];
    
    _selectedImg.center = button.center;
    
    //关闭动画
    [UIView commitAnimations];
    
    
}

//是否显示标签工具栏
- (void)showtabbar:(BOOL)show {

//    _tabbarView.frame.origin.x = -320;    错误
    CGRect frame = _tabbarView.frame;
    
    if (show) {
        //显示
        frame.origin.x = 0;
    }else {
        frame.origin.x = -320;
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.35];
    
    _tabbarView.frame = frame;
    
    [UIView commitAnimations];
    
}



@end

创建导航控制器父类,方便统一设置导航栏

BaseNavgationController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal1"] forBarMetrics:UIBarMetricsDefault];
    
}
HomeViewController.m

#import "HomeViewController.h"
#import "DetailViewController.h"
#import "MainTabbarController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"个人中心";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //创建按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.backgroundColor = [UIColor greenColor];
    button.frame = CGRectMake(90, 90, 90, 50);
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)buttonAction
{

    DetailViewController *detailCtrl = [[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailCtrl animated:YES];
    
    //隐藏工具栏
     MainTabbarController *mainCtrl = (MainTabbarController *)self.tabBarController;
    [mainCtrl showtabbar:NO];
}



@end
DetailViewController.m

#import "DetailViewController.h"
#import "MainTabbarController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // 视图在push过来的时候将标签栏移除
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"详情";
    
    self.view.backgroundColor = [UIColor redColor];
    
}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    
    //注意:视图控制器可以越级取得标签工具栏
    MainTabbarController *mainCtrl = (MainTabbarController *)self.tabBarController;
    
    //显示工具栏
    [mainCtrl showtabbar:YES];
    
}

SquleViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"广场";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];
    
}

SearchViewController.m

CommentViewController.m

MessageViewController.m

这三个文件与SquleViewController.m一致,只设置了title和backgroundColor



两种方式自定义标签工具栏