iPhone 自定义tabbar

自定义一个继承于UITabBarController的类,.h文件内容如下:

#import <UIKit/UIKit.h>
@interface CustomTabBar : UITabBarController {
    NSMutableArray *buttons;
    int currentSelectedIndex;
    UIImageView *slideBg;
}

@property (nonatomic,assign) int currentSelectedIndex;
@property (nonatomic,retain) NSMutableArray *buttons;

- (void)hideRealTabBar;
- (void)customTabBar;
- (void)selectedTab:(UIButton *)button;

@end

.m文件内容如下:

#import "CustomTabBar.h"

@implementation CustomTabBar

@synthesize currentSelectedIndex;
@synthesize buttons;

- (void)viewDidAppear:(BOOL)animated{
    slideBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottomfocus.png"]];
    [self hideRealTabBar];
    [self customTabBar];
}

- (void)hideRealTabBar{
    for(UIView *view in self.view.subviews){
        if([view isKindOfClass:[UITabBar class]]){
            view.hidden = YES;
            break;
        }
    }
}

- (void)customTabBar{
    UIImageView *imgView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tabbg2.png"]];
    imgView.frame = CGRectMake(0, 425, imgView.image.size.width, imgView.image.size.height);
    [self.view addSubview:imgView];
    slideBg.frame = CGRectMake(-30, self.tabBar.frame.origin.y, slideBg.image.size.width, slideBg.image.size.height);

    //创建按钮
    int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
    self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
    double _width = 320 / viewCount;
    double _height = self.tabBar.frame.size.height;
    for (int i = 0; i < viewCount; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*_width,self.tabBar.frame.origin.y, _width, _height);
        [btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        [self.buttons addObject:btn];
        [self.view  addSubview:btn];
        [btn release];
    }
    [self.view addSubview:slideBg];
    UIImageView *imgFront = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabitem.png"]];
    imgFront.frame = imgView.frame;
    [self.view addSubview:imgFront];
    [imgFront release];
    [imgView release];
    [self selectedTab:[self.buttons objectAtIndex:0]];

}

- (void)selectedTab:(UIButton *)button{
    if (self.currentSelectedIndex == button.tag) {
        
    }
    self.currentSelectedIndex = button.tag;
    self.selectedIndex = self.currentSelectedIndex;
    [self performSelector:@selector(slideTabBg:) withObject:button];
}

- (void)slideTabBg:(UIButton *)btn{
    [UIView beginAnimations:nil context:nil];  
    [UIView setAnimationDuration:0.20];  
    [UIView setAnimationDelegate:self];
    slideBg.frame = CGRectMake(btn.frame.origin.x - 30, btn.frame.origin.y, slideBg.image.size.width, slideBg.image.size.height);
    [UIView commitAnimations];
}

- (void) dealloc{
    [slideBg release];
    [buttons release];
    [super dealloc];
}
@end

使用这个类:

    // Override point for customization after application launch.
    FirstViewController *mainViewController = [[FirstViewController alloc] init];
    SecondViewController *searchViewController = [[SecondViewController alloc]init];
    ThirdViewController *myselfViewController = [[ThirdViewController alloc]init];
    ForthViewController *settingViewController = [[ForthViewController alloc]init];
    
    //隐藏tabbar所留下的黑边(试着注释后你会知道这个的作用)
    mainViewController.hidesBottomBarWhenPushed = true;
    searchViewController.hidesBottomBarWhenPushed = true;
    myselfViewController.hidesBottomBarWhenPushed = true;
    settingViewController.hidesBottomBarWhenPushed = true;
    
    mainViewController.title = @"首页";
    searchViewController.title = @"搜索";
    myselfViewController.title = @"我";
    settingViewController.title = @"设置";
    
    //创建导航
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainViewController ];
    UINavigationController *nav1 = [[ UINavigationController alloc] initWithRootViewController:searchViewController];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:myselfViewController];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:settingViewController];
    
    //创建数组
    NSMutableArray *controllers = [[NSMutableArray alloc]init];
    [controllers addObject:nav];
    [controllers addObject:nav1];
    [controllers addObject:nav2];
    [controllers addObject:nav3];
    
    //创建tabbar
    tabBarController = [[ CustomTabBar alloc] init];
    tabBarController.viewControllers = controllers;
    tabBarController.selectedIndex = 0;
    
    //显示
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值