Fork me on GitHub

UI-自定义TabBar

MyCustomTabBar.h文件

#import <UIKit/UIKit.h>

@interface MyCustomTabBar : UITabBarController

@end

MyCustomTabBar.m文件

#import "MyCustomTabBar.h"

#define kDeviceHeigh [UIScreen mainScreen].bounds.size.height

@interface MyCustomTabBar ()<UITabBarDelegate>

@end

@implementation MyCustomTabBar

-(void)viewDidLoad{

    [super viewDidLoad ];    

    //自定义TabBar

    UITabBar *tabBar = [[UITabBar alloc]initWithFrame:self.tabBar.frame];

    tabBar.userInteractionEnabled = YES;   

    [self.view addSubview:tabBar];

    tabBar.backgroundImage = [UIImage imageNamed:@"tabbg"];

    // 添加5个

    for (int i = 1 ; i <= 5 ; i ++ ) {

        // 获取按钮普通状态和选中状态的两个图片的名称

        NSString *imgName = [NSString stringWithFormat:@"TabBar%d", i];

        NSString *selImgName = [NSString stringWithFormat:@"TabBar%dSel", i];

        // 创建按钮

        UIButton *tabBtn = [[UIButton alloc]init ];

        CGFloat width = self.view.bounds.size.width / 5;

        // 设置按钮的frame

        tabBtn.frame =CGRectMake((i-1) * width , 0 , width , 44);

        if ( i == 3 ) {

            tabBtn.frame = CGRectMake(tabBtn.frame.origin.x , tabBtn.frame.origin.y - 20, tabBtn.frame.size.width, 44 + 20 );

            [tabBtn setBackgroundImage:[UIImage imageNamed:@"logoff_btn_n"] forState:UIControlStateNormal];

        }else{

        // 设置按钮普通状态和选中状态显示的图片

            [tabBtn setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];

            [tabBtn setBackgroundImage:[UIImage imageNamed:selImgName] forState:UIControlStateSelected ];

        }

        tabBtn.tag = 10 + i;

        [tabBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

        [tabBar addSubview:tabBtn];

    }

}

 

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

    for (int i = 1  ; i <= 5 ; i++ ) {

        UIButton *btn = (UIButton *)  [ self.view viewWithTag:10 + i];

        if (btn != button) {

            btn.selected = NO;

        }

    }

    button.selected = YES;

     if (button.tag == 13) {

        UIViewController *vc = [[UIViewController alloc]init];

        vc.view.backgroundColor = [UIColor orangeColor];

        [self presentViewController:vc animated:YES completion:nil];

        return;

    }

       self.selectedIndex = button.tag - 11;

}

@end

AppDelegate.m文件

#import "AppDelegate.h"

#import "MyCustomTabBar.h"

#import "FifthVC.h"

#import "SecondVc.h"

#import "ThirdVC.h"

#import "FourthVC.h"

#import "FirstVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

      //先创建五个控制器对象

    FirstVC *firstVC = [[FirstVC alloc]init];

    SecondVc  *secondVC = [[SecondVc alloc] init];

    ThirdVC *thirdVC = [[ThirdVC alloc]init];

    FourthVC *fourthVC = [[FourthVC alloc]init];

    FifthVC *fifthVC = [[FifthVC alloc]init];

  

    // 创建TabBar控制器对象

    MyCustomTabBar *tabBarVC = [[MyCustomTabBar alloc]init];

    // 设置Tabbar的子控制器对象, 需要注意的是,TabBar控制器最多只能显示五个子视图,如果超过5个,它会只显示4个,把其他的放More选项中

    tabBarVC.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC, fifthVC];

 

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = tabBarVC;

    [self.window makeKeyAndVisible];

    

    return YES;

}

posted @ 2016-04-15 22:07  极度恐慌_JG  阅读(229)  评论(0编辑  收藏  举报