#import "CustomTabBarItem.h"
@implementation CustomTabBarItem
+ (instancetype) itemWithFrame:(CGRect)frame title:(NSString *)title{
CustomTabBarItem *item = [CustomTabBarItem buttonWithType:UIButtonTypeCustom];
[item setFrame:frame];
[item setTitle:title forState:UIControlStateNormal];
[item.titleLabel setFont:[UIFont systemFontOfSize:17]];
[item setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[item setBackgroundImage:[UIImage imageNamed:@"n"] forState:UIControlStateNormal];
[item setBackgroundImage:[UIImage imageNamed:@"s"] forState:UIControlStateSelected];
return item;
}
@end
#import <UIKit/UIKit.h>
@class CustomTabBar;
//自定义协议,实现与其他类通信
@protocol CustomTabBarDelegate <NSObject>
- (void) tabBar:(CustomTabBar *)tabBar itemSelectedAtIndex:(NSInteger)index;
@end
@interface CustomTabBar : UIView
@property(nonatomic, weak) id<CustomTabBarDelegate> delegate;
+ (instancetype) tabBarWithTites:(NSArray *)titles;
@end
#import "CustomTabBar.h"
#import "CustomTabBarItem.h"
static const CGFloat TabBarHeight = 44;
@interface CustomTabBar()
@property(nonatomic, strong) NSArray *titles;
@property(nonatomic, weak) CustomTabBarItem *preSelectedItem;
@end
@implementation CustomTabBar
+ (instancetype) tabBarWithTites:(NSArray *)titles{
CustomTabBar *tabBar = [[CustomTabBar alloc] init];
[tabBar setTitles:titles];
[tabBar setBackgroundColor:[UIColor grayColor]];
return tabBar;
}
- (void)willMoveToSuperview:(UIView *)newSuperview{
self.frame = CGRectMake(0, CGRectGetMaxY(newSuperview.frame) - TabBarHeight, CGRectGetWidth(newSuperview.frame), TabBarHeight);
//必须是在这里创建items,不然无法获取其frame信息
[self addItems];
}
- (void) addItems{
CGFloat count = self.titles.count;
CGFloat height = CGRectGetHeight(self.bounds);
CGFloat width = CGRectGetWidth(self.bounds) / count;
for (NSInteger i = 0 ; i < count ; i++){
CustomTabBarItem *item = [CustomTabBarItem itemWithFrame:CGRectMake(i * width, 0, width, height) title:self.titles[i]];
[item setTag:i];
[self addSubview:item];
//设置监听,点击按钮时
[item addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)itemSelected:(CustomTabBarItem *)send{
[self.preSelectedItem setSelected:NO];
[send setSelected:YES];
[self setPreSelectedItem:send];
[self.delegate tabBar:self itemSelectedAtIndex:send.tag];
}
@end
#import "MainViewController.h"
#import "CustomTabBar.h"
@interface MainViewController ()<CustomTabBarDelegate>
@property(nonatomic, strong) UILabel *label;
@property(nonatomic, strong) NSArray *titles;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.titles = @[@"OC", @"JAVA", @"PHP"];
CustomTabBar *tabBar = [CustomTabBar tabBarWithTites:self.titles];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 200)];
[self.label setBackgroundColor:[UIColor grayColor]];
[self.label setFont:[UIFont systemFontOfSize:60]];
[self.label setTextColor:[UIColor yellowColor]];
[self.label setTextAlignment:NSTextAlignmentCenter];
[self.label setText:@"OC"];
[self.view addSubview:tabBar];
[self.view addSubview:self.label];
[tabBar setDelegate:self];
}
- (void)tabBar:(CustomTabBar *)tabBar itemSelectedAtIndex:(NSInteger)index{
[self.label setText:self.titles[index]];
}
@end