iOS自定义TabBar一般有两种方式:
创建一个类继承字UITabBar,在layoutSubviews方法中重新调整按钮的位置,再通过[self setValue:tabBar forKeyPath:@"tabBar"]方法,利用KVC设置TabBar,但是iOS 13后苹果粑粑不鼓励使用KVC。
这篇文章主要讲第二种方法:创建一个继承UIView的类WSTabBar,然后把系统UITabBar上的UITabBarItem移除,然后把WSTabBar加到系统TabBar的位置上,效果图如下:
tabbar截图.png
一、自定义一个TabBar,其实就是自定义个UIView,然后布局5个按钮,为了方便我请设计把图片和文字都切在一起了。
#import "WSTabBar.h"
#import "WSTabBarButton.h"
@interface WSTabBar ()
/// 记录上一个按钮
@property (nonatomic, weak) UIButton *preButton;
@end
@implementation WSTabBar
+ (instancetype)tabBarWithSBNames:(NSArray *)sbNames {
WSTabBar *tabBar = [[WSTabBar alloc] init];
//创建图片对应的按钮 -- tabBarItem
for (NSString *sbName in sbNames) {
WSTabBarButton *btn = [WSTabBarButton buttonWithType:UIButtonTypeCustom];
//记录按钮的索引
btn.tag = tabBar.subviews.count;
[tabBar addSubview:btn];
//加载按钮中显示的图片
NSString *imgName = [NSString stringWithFormat:@"TabBar_%@",sbName];
NSString *selectedImgName = [NSString stringWithFormat:@"TabBar_%@_selected",sbName];
//设置按钮的图片
[btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:selectedImgName] forState:UIControlStateSelected];
[btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
//给按钮注册点击事件
[btn addTarget:tabBar action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
}
//让第一个按钮选中
UIButton *btn = [tabBar.subviews firstObject];
[tabBar btnClick:btn];
return tabBar;
}
//点击按钮
- (void)btnClick:(UIButton *)sender {
//让上一个按钮不选中
self.preButton.selected = NO;
//让当前按钮选中
sender.selected = YES;
//记录上一个按钮
self.preButton = sender;
//合适的时机调用代理方法
if ([self.delegate respondsToSelector:@selector(tabBarDidClickedButton:selectedIndex:)]) {
[self.delegate tabBarDidClickedButton:self selectedIndex:sender.tag];
}
}
//设置按钮的位置
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat btnW = self.bounds.size.width / self.subviews.count;
CGFloat btnX = 0;
CGFloat btnH;
CGFloat btnY;
for (int i = 0; i < self.subviews.count; i++) {
UIButton *btn = self.subviews[i];
//设置按钮的位置
if (i == 2) { // 中间不规则按钮位置计算
btnY = -7;
btnH = 56;
} else { // 普通按钮位置计算
btnY = 0;
btnH = self.bounds.size.height;
}
btnX = i * btnW;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
}
@end
二、再自定义一个UITabBarController,将系统的UITabBarItem移除,防止重影,然后将自定义UIView加到UITabBar。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view;
[self configureTabbar];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// 移除系统TabBar的UITabBarItem
for (UIView *view in self.tabBar.subviews) {
if (![view isKindOfClass:[WSTabBar class]]) {
[view removeFromSuperview];
}
}
}
/// 自定义tabbar
- (void)configureTabbar {
// tabbar图标名称
NSArray *sbNames = @[
@"Home",
@"Live",
@"EPG",
@"VIP",
@"Mine"
];
//2 设置自定义tabBar
WSTabBar *tabBar = [WSTabBar tabBarWithSBNames:sbNames];
//设置代理
tabBar.delegate = self;
// 加到系统tabbar位置
[self.tabBar addSubview:tabBar];
tabBar.frame = self.tabBar.bounds;
}
#pragma mark - WSTabbarDelegate
//自定义tabBar的代理方法
- (void)tabBarDidClickedButton:(WSTabBar *)tabBar selectedIndex:(NSInteger)selectedIndex {
//让tabBarController中的对应子控制器显示
self.selectedIndex = selectedIndex;
}
三、去掉按钮的高亮状态,写一个继承UIButton的类WSTabBarButton
#import "WSTabBarButton.h"
@implementation WSTabBarButton
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)setHighlighted:(BOOL)highlighted {
}
@end