iOS 自定义UITabBar

首先 隐藏系统的TabBar视图
自定义一个TabBar视图(高度49像素)
将自定义的视图添加背景图片
定义一个选中时的图片,选中时,实现简单的移动动画效果
关键代码如下:
(创建“UITabBarController”类文件“MainBarViewController.h/m”);
“AppDelegate.m”

MainBarViewController *mainVeiw = [[MainBarViewController alloc] init];

self.window.rootViewController = mainVeiw;


“MainBarViewController.h”

@interface MainBarViewController : UITabBarController{

    UIImageView *selectView;

}

“MainBarViewController.m”

@implementation MainBarViewController

– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

        self.tabBar.hidden = YES;//隐藏系统TabBar

    }

    return self;

}

//装载视图控制器

-(void)loadViewControllers{

    OneViewController *oneView = [[OneViewController alloc] init];

    oneView.title = @”one”;

    UINavigationController *oneNav = [[UINavigationController alloc] initWithRootViewController:oneView];

    

    TwoViewController *twoView = [[TwoViewController alloc] init];

    twoView.title = @”two”;

    UINavigationController *twoNav = [[UINavigationController alloc] initWithRootViewController:twoView];

    

    NSArray *viewControllers = @[oneNav,twoNav];

    

    [self setViewControllers:viewControllers];

}

-(void)loadCustomTabBarView{

    /*

     *层次:背景(最下)、选中图片(中间)、按钮(最上)

     */

    

    //初始化自定义TabBar背景

    UIImageView *tabBarGB = [[UIImageView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];

    tabBarGB.userInteractionEnabled = YES;

    tabBarGB.image = [UIImage imageNamed:@”barbg.png”];

    [self.view addSubview:tabBarGB];

    

    //初始化自定义选中背景

    selectView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 49.0/245.0/2, 53, 45)];

    selectView.image = [UIImage imageNamed:@”select.png”];

    [tabBarGB addSubview:selectView];

    

    //初始化自定义TabBarItem -> UIButton

    float coordinateX = 0;

    for (int index=0; index<2; index++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        button.tag = index;

        button.frame = CGRectMake(14+coordinateX, 49.0/220, 42, 40);

        NSString *imageName = [NSString stringWithFormat:@”%d.png”,index+1];//按钮图片

        [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

        [tabBarGB addSubview:button];

        [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];

        coordinateX += 62;

    }

}

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

    self.selectedIndex = button.tag;

    [UIView beginAnimations:nil context:NULL];//动画

    selectView.frame = CGRectMake(10+button.tag*62, 49.0/245.0/2, 53, 45);

    [UIView commitAnimations];

}

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self loadViewControllers];

    

    [self loadCustomTabBarView];

    

}

demo下载地址:http://www.kuaipan.cn/file/id_82777686379857034.htm

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注