UITabBarController中自定义UITabBar方法2

思路:

1.将原来的UITabBarController中的UITabBar隐藏起来;

2.创建一个UIView,上面放入各个按钮;

3.点击按钮设置UITabBarController显示哪个控制器


代码:MainViewController类是继承了UITabBarController的,以下为伪代码

MainViewController.h文件

@interface MainViewController : UITabBarController {
    UIView *tabBarView;//创建一个UIView,用于放入各个按钮
}

MainViewController.m文件

#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.tabBar setHidden:YES];//将原来的UITabBarController中的UITabBar隐藏起来;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self _initViewController];//初始化UITabBarController中的控制器
    [self _initTabbarView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化子控制器
- (void)_initViewController {
    AViewController *aViewCor = [[AViewController alloc] init];
    BViewController *bViewCor = [[BViewController alloc] init];
    CViewController *cViewCor = [[CViewController alloc] init];
    DViewController *dViewCor = [[DViewController alloc] init];
    EViewController *eViewCor = [[EViewController alloc] init];
    
    NSArray *views = @[aViewCor,bViewCor,cViewCor,dViewCor,eViewCor];//将控制器view放入NavigationController中
    NSMutableArray *viewControllers = [NSMutableArray arrayWithCapacity:5];
    for (UIViewController *viewController in views) {
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:viewController];
        [viewControllers addObject:nav];
        [nav release];
    }
    self.viewControllers = viewControllers;
}

//创建自定义tabBar
- (void)_initTabbarView {
    _tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 49, ScreenWidth, 49)];//49为tabBar的高度
    _tabbarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background.png"]];//设置tabBar的背景颜色
    [self.view addSubview:_tabbarView];
    
    NSArray *backgroud = @[@"tabbar_aa.png",@"tabbar_bb.png",@"tabbar_cc.png",@"tabbar_dd.png",@"tabbar_ee.png"];//tabBarItem中的按钮
    
    NSArray *heightBackground = @[@"tabbar_aa_highlighted.png",@"tabbar_bb_highlighted.png",@"tabbar_cc_highlighted.png",@"tabbar_dd_highlighted.png",@"tabbar_ee_highlighted.png"];//tabBarItem中高亮时候的按钮
    //将按钮添加到自定义的_tabbarView中,并为按钮设置tag(tag从0开始)
    for (int i=0; i

你可能感兴趣的:(控件使用)