#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 普通带圆点
时间: 2024-10-16 16:37:31