.h文件
#import <UIKit/UIKit.h>
@class MyTabBar;
@protocol MyTabBarDelegate <UITabBarDelegate>
@optional
- (void)tabBarDidClickPlusButton:(MyTabBar *)tabBar;
@end
@interface MyTabBar : UITabBar
@property (nonatomic, weak) id<MyTabBarDelegate> delegate;
@end
.m文件
#import "MyTabBar.h"
@interface MyTabBar()
@property (nonatomic, weak) UIButton *plusBtn;
@end
@implementation MyTabBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *plusBtn = [[UIButton alloc] init];
[plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
[plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
[plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
[plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
plusBtn.size = plusBtn.currentBackgroundImage.size;
[plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:plusBtn];
self.plusBtn = plusBtn;
}
return self;
}
- (void)plusClick
{
if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
[self.delegate tabBarDidClickPlusButton:self];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.plusBtn.centerX = self.width * 0.5;
self.plusBtn.centerY = self.height * 0.5;
CGFloat tabbarButtonW = self.width / 5;
CGFloat tabbarButtonIndex = 0;
for (UIView *child in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([child isKindOfClass:class]) {
child.width = tabbarButtonW;
child.x = tabbarButtonIndex * tabbarButtonW;
tabbarButtonIndex++;
if (tabbarButtonIndex == 2) {
tabbarButtonIndex++;
}
}
}
}
@end
在tabBarController中使用自定义的tabBar
#import "MyTabBarViewController.h"
#import "fistViewController.h"
#import "secondViewController.h"
#import "thirdViewController.h"
#import "forthViewController.h"
#import "MyTabBar.h"
@interface MyTabBarViewController ()<MYTabBarDelegate>
@end
@implementation MyTabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self addChildVc:[[fistViewController alloc] init] title:@"首页" image:@"1" selectedImage:@"1"];
[self addChildVc:[[secondViewController alloc] init] title:@"社区" image:@"2" selectedImage:@"2"];
[self addChildVc:[[thirdViewController alloc] init] title:@"消息" image:@"3" selectedImage:@"3"];
[self addChildVc:[[forthViewController alloc] init] title:@"咨询" image:@"4" selectedImage:@"4"];
MyTabBar *tabBar = [[MyTabBar alloc] init];
tabBar.translucent = NO;
tabBar.myTabBarDelegate = self;
[self setValue:tabBar forKey:@"tabBar"];
}
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
childVc.title = title;
childVc.tabBarItem.image = [UIImage imageNamed:image];
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];
[childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} forState:UIControlStateSelected];
UINavigationController *navigationVc = [[UINavigationController alloc] initWithRootViewController:childVc];
[self addChildViewController:navigationVc];
}
- (void)tabBarDidClickPlusButton:(MyTabBar *)tabBar
{
NSLog(@"点击了++号");
}
@end