自定义UITabBar替换系统默认的,目的是为了在UITabBar中间位置添加一个“+号按钮”
1、自定义WBTabBar,让其继承自UITabBar,并定义点击事件代理方法。
.h方法里面
#import <UIKit/UIKit.h>
@class WXReleaseTabBar;
@protocol WXReleaseTabBarDelegate <WXReleaseTabBarDelegate>
@optional
- (void)tabBarDidClickPlusButton:(WXReleaseTabBar *)tabBar;
@end
@interface WXReleaseTabBar : UITabBar
@property (nonatomic, weak) id<WXReleaseTabBarDelegate> tabBarDelegate;
@end
.m方法里面
//定义一个按钮
@property (nonatomic, strong) UIButton *plusButton;
然后初始化
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *plusBtn = [[UIButton alloc] init];
[plusBtn setBackgroundImage:[UIImage imageNamed:@"shareicon"] forState:UIControlStateNormal];
[plusBtn setBackgroundImage:[UIImage imageNamed:@"shareicon"] forState:UIControlStateHighlighted];
[plusBtn setImage:[UIImage imageNamed:@"shareicon"] forState:UIControlStateNormal];
[plusBtn setImage:[UIImage imageNamed:@"shareicon"] forState:UIControlStateHighlighted];
plusBtn.size = plusBtn.currentBackgroundImage.size;
[plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:plusBtn];
self.plusButton = plusBtn;
}
return self;
}
//添加约束
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.width;
CGFloat height = self.height;
self.plusButton.center = CGPointMake(width * 0.5, height * 0.5);
CGRect tempRect = self.plusButton.frame;
tempRect.origin.y = -20;
self.plusButton.frame = tempRect;
int index = 0;
CGFloat tabBarButtonW = width / 5;
CGFloat tabBarButtonH = height;
CGFloat tabBarButtonY = 0;
for (UIView *tabBarButton in self.subviews) {
if (![NSStringFromClass(tabBarButton.class) isEqualToString:@"UITabBarButton"]) continue;
CGFloat tabBarButtonX = index * tabBarButtonW;
if (index >= 2) {
tabBarButtonX += tabBarButtonW;
}
tabBarButton.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonW, tabBarButtonH);
index++;
}
[self bringSubviewToFront:self.plusButton];
}
//重写系统的hitTest方法让处于tabbar外部的按钮部分也可以被点击
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (self.isHidden == NO) { // 当前界面 tabBar显示
CGPoint newPoint = [self convertPoint:point toView:self.plusButton];
if ( [self.plusButton pointInside:newPoint withEvent:event]) { // 点 属于按钮范围
return self.plusButton;
}else{
return [super hitTest:point withEvent:event];
}
}else {
return [super hitTest:point withEvent:event];
}
}
//被点击的方法
- (void)plusClick
{
// 通知代理
if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
[self.tabBarDelegate tabBarDidClickPlusButton:self];
}
}
2、tabBar是UITabBarController的只读成员变量(属性),是不让修改的,在UITabBarController.m中的操作如下:
@interface WXTabbarViewController ()<UITabBarDelegate>
@end
3、然后在UITabBarController.m初始化里面我们可以使用KVC的方式,更换系统自带的UITabBar,实现代码如下:
xxxTabBar(自定义的tabbar) *tabBar = [[xxxTabBar alloc] init]; xxxTabBar.tabBarDelegate= self;
[self setValue:tabBar forKeyPath:@"tabBar"];
4、在UITabBarController.m实现点击的代理方法
- (void)tabBarDidClickPlusButton:(WXReleaseTabBar *)tabBar
{
NSLog(@"发布按钮");
// ComposeViewController *composeViewController= [[ComposeViewController alloc] init];
// UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:composeViewController];
// [self presentViewController:navigationController animated:YES completion:nil];
}