分析图片中中间Tabbar:
可通过两个UIButton(选中和未选中)实现,之后就是通过layoutSubViews方法中重新摆放这个几个的位置
之后将语言转换为代码:
.h
#import <UIKit/UIKit.h>
@interface GCSTabBar : UITabBar
@end
.m
#import "GCSTabBar.h"
@interface GCSTabBar ()
/**发布按钮*/
@property (nonatomic, weak) UIButton *publishButton;
@end
@implementation GCSTabBar
/**
* http://www.jianshu.com/p/7e47da62899c
*/
/**
* 一般声明一些控件
*
* @param frame frame description
*
* @return return value description
*/
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
[self addSubview:publishButton];
self.publishButton = publishButton;
}
return self;
}
#pragma mark - 重新计算子控件位置
-(void)layoutSubviews {
[super layoutSubviews];
//设置发布的按钮,
self.publishButton.frame = CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);
//将自定义按钮放到中间
self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
//设置其他UITabBarButton的frame
CGFloat buttonY = 0;
CGFloat buttonW = self.frame.size.width / 5;
CGFloat buttonH = self.frame.size.height;
NSInteger index = 0;
for (UIView *button in self.subviews) {
if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
//计算按钮x的值,当是自定义tabbar则+1
CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
//增加索引
index++;
}
}
@end
使用方式:
//添加自定义TabBar
[self setValue:[[GCSTabBar alloc] init] forKeyPath:@"tabBar"];