码迷,mamicode.com
首页 > 其他好文 > 详细

MJLTabbar

时间:2015-10-06 22:03:22      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
#import "TabViewController.h"
#import "MJLTabBar.h"
#import "UIImage+MJL.h"

@interface TabViewController ()<MJLTabBarDelegate>

@property (nonatomic,strong)MJLTabBar *myTabBar;

@end

@implementation TabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //创建子页面
    UIViewController *home = [UIViewController new];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:home];
    home.view.backgroundColor = [UIColor whiteColor];
    
    UIViewController *message = [UIViewController new];
    UINavigationController *messageNav= [[UINavigationController alloc]initWithRootViewController:message];
    message.view.backgroundColor = [UIColor orangeColor];
    
    
    UIViewController *discover = [UIViewController new];
    UINavigationController *discoverNav = [[UINavigationController alloc]initWithRootViewController:discover];
    
    UIViewController *me = [UIViewController new];
    UINavigationController *meNav = [[UINavigationController alloc]initWithRootViewController:me];
    self.viewControllers = @[homeNav,messageNav,discoverNav,meNav];


    //设置数据
    [self setUpTabBar:homeNav title:@"首页" selectIma:@"tabbar_home" selectedIma:@"tabbar_home_selected"];
    [self setUpTabBar:messageNav title:@"消息" selectIma:@"tabbar_message_center" selectedIma:@"tabbar_message_center_selected"];
    [self setUpTabBar:discoverNav title:@"广场" selectIma:@"tabbar_discover" selectedIma:@"tabbar_discover_selected"];
    [self setUpTabBar:meNav title:@"" selectIma:@"tabbar_profile" selectedIma:@"tabbar_profile_selected"];
    
    //添加导航
    self.tabBar.hidden = YES;
    self.myTabBar = [MJLTabBar new];
    self.myTabBar.height = 49;
    [self.view addSubview:self.myTabBar];//前三行代码顺序不可改
    
    //中间按钮数据以及代理
    UIImage *image = [UIImage addImage:[UIImage imageNamed:@"1"] toImage:[UIImage imageNamed:@"1_1"]];
    UIImage *imageSel = [UIImage addImage:[UIImage imageNamed:@"2"] toImage:[UIImage imageNamed:@"2_2"]];
    UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"" image:image selectedImage:imageSel];
    self.myTabBar.tabBarItems=@[homeNav.tabBarItem,messageNav.tabBarItem,item,discoverNav.tabBarItem,meNav.tabBarItem];
    self.myTabBar.delegate = self;
    
    
}
-(void)mTabBar:(MJLTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to{
    
    NSLog(@"%zd",to);
    if (to <=2) {
        self.selectedIndex = to-1;
    }else{
        self.selectedIndex = to-2;
    }
    
}
-(void)mTabBarDidClickMiddleButton:(MJLTabBar *)tabBar{
    
}
-(void)setUpTabBar:(UIViewController *)vc title:(NSString *)title selectIma:(NSString *)selectIma selectedIma:(NSString *)selectedIma{
    vc.title =title;
    vc.tabBarItem.image = [UIImage imageNamed:selectIma];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedIma];
}

@end
MJLTabBar 终极版
技术分享
#import "ViewController1.h"
#import "MJLTabBar.h"
#import "UIImage+MJL.h"

@interface ViewController1 ()<MJLTabBarDelegate>

@property (nonatomic,strong)MJLTabBar *myTabBar;

@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建子页面
    UIViewController *home = [UIViewController new];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:home];
    home.view.backgroundColor = [UIColor whiteColor];
    
    UIViewController *message = [UIViewController new];
    UINavigationController *messageNav= [[UINavigationController alloc]initWithRootViewController:message];
    message.view.backgroundColor = [UIColor orangeColor];
    
    
    UIViewController *discover = [UIViewController new];
    UINavigationController *discoverNav = [[UINavigationController alloc]initWithRootViewController:discover];
    
    UIViewController *me = [UIViewController new];
    UINavigationController *meNav = [[UINavigationController alloc]initWithRootViewController:me];
    self.viewControllers = @[homeNav,messageNav,discoverNav,meNav];
    
    
    //设置数据
    [self setUpTabBar:homeNav title:@"" selectIma:@"tabbar_home" selectedIma:@"tabbar_home_selected"];
    [self setUpTabBar:messageNav title:@"" selectIma:@"tabbar_message_center" selectedIma:@"tabbar_message_center_selected"];
    [self setUpTabBar:discoverNav title:@"" selectIma:@"tabbar_discover" selectedIma:@"tabbar_discover_selected"];
    [self setUpTabBar:meNav title:@"" selectIma:@"tabbar_profile" selectedIma:@"tabbar_profile_selected"];
    
    //添加导航
    self.tabBar.hidden = YES;
    self.myTabBar = [MJLTabBar new];
    self.myTabBar.height = 49;
    [self.view addSubview:self.myTabBar];//前三行代码顺序不可改
    
   
    self.myTabBar.tabBarItems=@[homeNav.tabBarItem,messageNav.tabBarItem,discoverNav.tabBarItem,meNav.tabBarItem];
    self.myTabBar.delegate = self;
    
    homeNav.tabBarItem.badgeValue = @"1234";
    messageNav.tabBarItem.badgeValue = @"5";


}
-(void)mTabBar:(MJLTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to{
    
    NSLog(@"%zd",to);
    if (to <=2) {
        self.selectedIndex = to-1;
    }else{
        self.selectedIndex = to-2;
    }
    
}
-(void)mTabBarDidClickMiddleButton:(MJLTabBar *)tabBar{
    
}
-(void)setUpTabBar:(UIViewController *)vc title:(NSString *)title selectIma:(NSString *)selectIma selectedIma:(NSString *)selectedIma{
    vc.title =title;
    vc.tabBarItem.image = [UIImage imageNamed:selectIma];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedIma];
}

@end
MJLTabBar 普通带圆点

 

MJLTabbar

标签:

原文地址:http://www.cnblogs.com/coderMJL/p/4857703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!