使用方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSArray *classNameArray = @[@"HomeViewController",@"SecondViewController",@"ThirdViewController",@"ForthViewController",@"FifthViewController"];
NSMutableArray *subViewControllers = [NSMutableArray array];
for (NSString *title in TITLEARRAY) {
NSInteger index = [TITLEARRAY indexOfObject:title];
UIViewController *vc = [[NSClassFromString(classNameArray[index]) alloc] init];
vc.title = title;
LYNavigationController *nav = [[LYNavigationController alloc]initWithRootViewController:vc];
nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -4);
[subViewControllers addObject:nav];
}
LYTabBarController *tabVC = [[LYTabBarController alloc]initWithViewControllers:subViewControllers];
self.window.rootViewController = tabVC;
return YES;
}
实现代码
#define TITLEARRAY @[@"首页",@"成绩关注",@"家长中心",@"家长社区",@"关于我们"]
#import <UIKit/UIKit.h>
#import "LYTabBar.h"
@interface LYTabBarController : UITabBarController<LYTabBarDelegate>
- (instancetype)initWithViewControllers:(NSArray *)controllers;
@end
#import "LYTabBarController.h"
@interface LYTabBarController () <LYTabBarDelegate>
@property (nonatomic, weak) UIButton *selectedBtn;
@end
@implementation LYTabBarController
- (instancetype)initWithViewControllers:(NSArray *)controllers{
if (self = [super init]) {
self.viewControllers = controllers;
CGRect rect = self.tabBar.bounds;
LYTabBar *myView = [[LYTabBar alloc] init];
myView.delegate = self;
myView.frame = rect;
[self.tabBar addSubview:myView];
for (int i=0; i<self.viewControllers.count; i++) {
NSString *imageName = [NSString stringWithFormat:@"tabbar_n%d", i];
NSString *imageNameSel = [NSString stringWithFormat:@"tabbar_h%d", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImage *imageSel = [UIImage imageNamed:imageNameSel];
[myView addButtonWithImage:image selectedImage:imageSel andTitle:[TITLEARRAY objectAtIndex:i]];
}
self.tabBar.barTintColor = [UIColor colorWithRed:38/255.0 green:38/255.0 blue:38/255.0 alpha:1];
self.tabBar.selectedImageTintColor = [UIColor whiteColor];
}
return self;
}
- (void)tabBar:(LYTabBar *)tabBar selectedFrom:(NSInteger)from to:(NSInteger)to {
self.selectedIndex = to;
}
@end
#import <UIKit/UIKit.h>
@class LYTabBar;
@protocol LYTabBarDelegate <NSObject>
- (void)tabBar:(LYTabBar *)tabBar selectedFrom:(NSInteger) from to:(NSInteger)to;
@end
@interface LYTabBar : UIView
@property(nonatomic,weak) id<LYTabBarDelegate> delegate;
-(void)addButtonWithImage:(UIImage *)image selectedImage:(UIImage *) selectedImage andTitle:(NSString *)title;
@end
@interface LYTabBarButton : UIButton
@end
#import "LYTabBar.h"
@interface LYTabBar ()
@property (nonatomic, weak) UIButton *selectedBtn;
@end
@implementation LYTabBar
- (void)addButtonWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage andTitle:(NSString *)title{
LYTabBarButton *btn = [[LYTabBarButton alloc] init];
[btn setImage:image forState:UIControlStateNormal];
[btn setImage:selectedImage forState:UIControlStateSelected];
[self addSubview:btn];
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
if (self.subviews.count == 1) {
[self clickBtn:btn];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
NSInteger count = self.subviews.count;
for (int i = 0; i < count; i++) {
UIButton *btn = self.subviews[i];
btn.tag = i;
CGFloat x = i * self.bounds.size.width / count;
CGFloat y = 0;
CGFloat width = self.bounds.size.width / count;
CGFloat height = self.bounds.size.height;
btn.frame = CGRectMake(x, y, width, height);
}
}
- (void)clickBtn:(UIButton *)button {
self.selectedBtn.selected = NO;
button.selected = YES;
self.selectedBtn = button;
if ([self.delegate respondsToSelector:@selector(tabBar:selectedFrom:to:)]) {
[self.delegate tabBar:self selectedFrom:self.selectedBtn.tag to:button.tag];
}
}
@end
@implementation LYTabBarButton
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
return CGRectMake(24 , 10 , 17 , 17);
}
@end