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<backgroud.count; i++) {
        NSString *backImage = backgroud[i];
        NSString *heightImage = heightBackground[i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((64-30)/2+(i*64), (49-30)/2, 30, 30);
        button.tag = i;
        [button setImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:heightImage] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
        [_tabbarView addSubview:button];
    }
}

//点击按钮后显示哪个控制器界面
- (void)selectedTab:(UIButton *)button {
    self.selectedIndex = button.tag;
}
@end

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值