CustomTabBarViewController

//  AppDelegate.m
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "AppDelegate.h"
#import "CustomTabBarViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    CustomTabBarViewController *customTabBar= [[CustomTabBarViewController alloc]init];
    self.window.rootViewController=customTabBar;
    return YES;
}

//
//  ViewController.h
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

//  ViewController.m
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

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

@end
//  CustomTabBarViewController.h
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomTabBarViewController : UITabBarController

@end

//
//  CustomTabBarViewController.m
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "CustomTabBarViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
@interface CustomTabBarViewController ()

@end

@implementation CustomTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //隐藏系统tabbar
    self.tabBar.hidden=YES;

    //创建视图控制器数组
    [self creatViewController];

    //
    //创建tabbar
    UIImageView *bgView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabbg"]];
    bgView.tag=499;
    bgView.frame=CGRectMake(0, self.view.frame.size.height-49, self.view.frame.size.width, 49);
    [self.view addSubview:bgView];
    self.view.backgroundColor=[UIColor cyanColor];
    //打开用户交互使能
    bgView.userInteractionEnabled=YES;
    CGFloat space =(self.view.frame.size.width-4*30)/5;

    for (int i=0; i<4; i++) {
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(space+i*(30+space),9, 30, 30);
        NSString *imageName =[NSString stringWithFormat:@"tab_%i",i];
        NSString *selectimageName =[NSString stringWithFormat:@"tab_c%i",i];
        //设置正常状态图片
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        //设置选中状态图片
        [btn setBackgroundImage:[UIImage imageNamed:selectimageName] forState:UIControlStateSelected];
        //添加点击事件
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag=100+i;

        //设置选中状态的btn
        if(btn.tag==100)
        {
            btn.selected=YES;
        }
        [bgView addSubview:btn];
    }

    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(space, 40, 30, 4)];
    label.backgroundColor=[UIColor orangeColor];
    label.tag=498;
    [bgView addSubview:label];

}

-(void)btnClicked:(UIButton *)btn
{

    //self.selectedIndex=3;
    //记录选中的button
    NSInteger index =btn.tag-100;
    UIView *bgView =[self.view viewWithTag:499];
    for (UIView *view in bgView.subviews) {
        if(view.tag!=498)
        {
            if(btn.tag==view.tag)
            {
                //btn.selected=YES;
                ((UIButton *)view).selected=YES;

            }
            else{
                //btn.selected=NO;
                ((UIButton *)view).selected=NO;
            }
        }
    }
    self.selectedIndex=index;

    UILabel *label =(UILabel *)[bgView viewWithTag:498];
    CGFloat space =(self.view.frame.size.width-4*30)/5;
    [UIView animateWithDuration:0.3 animations:^(void){
        CGRect frame =label.frame;
        frame.origin.x  =space+(space +30)*index;
        label.frame=frame;
    }];
}

-(void)creatViewController
{
    FirstViewController *firstVC=[[FirstViewController alloc]init];
    SecondViewController *secondVC=[[SecondViewController alloc]init];
    ThirdViewController *thirdVC=[[ThirdViewController alloc]init];
    FourthViewController *fourthVC =[[FourthViewController alloc]init];

    NSArray *viewController =[NSArray arrayWithObjects:firstVC,secondVC,thirdVC,fourthVC, nil];
    self.viewControllers=viewController;
    //self.tabBarController.viewControllers=viewController;
}

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

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
//  FourthViewController.h
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FourthViewController : UIViewController

@end

//  FourthViewController.m
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "FourthViewController.h"

@interface FourthViewController ()

@end

@implementation FourthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor redColor];
}

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

//  ThirdViewController.h
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@end

//  ThirdViewController.m
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor blueColor];
}

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

//  SecondViewController.h
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end

//  SecondViewController.m
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];
}

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

//  FirstViewController.h
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

//  FirstViewController.m
//  CustomTabBar
//
//  Created by qianfeng on 15/7/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor yellowColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
时间: 2024-10-24 17:41:17

CustomTabBarViewController的相关文章

TabBarViewController的创建以及渐变隐藏

// CustomTabBarViewController.h #import <UIKit/UIKit.h> @interface CustomTabBarViewController : UITabBarController + (void)deallocTabbar; + (id)defaultsTabBar; - (void)selectBUttonWithIndex:(NSInteger)index; -(void)setTabBarHidden:(BOOL)Bool; @end /