在实际开发中有时遇见特殊的TabBar,系统已无法满足,此时可自定义解决开发需求。话不多说,直接上代码,有问题欢迎指正。
1、先自定义一个继承于UIButton的TBCustomTabBar
@interface TBCuntomButton : UIButton
@property (nonatomic , strong) UIButton *iconButton;
@property (nonatomic , strong) UILabel *textLabel;
@end
#import "TBCuntomButton.h"
#import "UIButton+Image.h"
@implementation TBCuntomButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self.iconButton removeFromSuperview];
[self addSubview:self.iconButton];
[self.iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.right.mas_equalTo(0);
}];
}
return self;
}
- (UIButton *)iconButton {
if (!_iconButton) {
_iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
_iconButton.userInteractionEnabled = NO;
}
return _iconButton;
}
- (UILabel *)textLabel {
if (!_textLabel) {
_textLabel = [[UILabel alloc] init];
[_textLabel setTextColor:[UIColor colorWithHexString:@"#2F2F2F"]];
[_textLabel setTextAlignment:NSTextAlignmentLeft];
[_textLabel setFont:JiangXiZhuoKaiFont(12)];
}
return _textLabel;
}
- (void)setHighlighted:(BOOL)highlighted {
}
@end
2、自定义View
#import <UIKit/UIKit.h>
@class TBCustomTabBar;
NS_ASSUME_NONNULL_BEGIN
@protocol TBCustomTabBarDelegate <NSObject>
- (void)tabBar:(TBCustomTabBar *)tabBar index:(NSInteger) index;
@end
@interface TBCustomTabBar : UIView
@property (nonatomic , strong) NSArray *items;
@property (nonatomic , weak) id<TBCustomTabBarDelegate> delegate;
@property (nonatomic , strong) UIImageView *iconImageView;
@property (nonatomic , strong) UIButton *exitButton;
@property (nonatomic , strong) UIImageView *exitImageView;
@property (nonatomic , strong) UILabel *exitLabel;
@end
#import "TBCustomTabBar.h"
#import "TBCuntomButton.h"
#import "UIButton+Image.h"
@interface TBCustomTabBar ()
@property (nonatomic , weak) UIButton *seletedButton;
@end
@implementation TBCustomTabBar
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (void)setItems:(NSArray *)items {
_items = items;
for (int i = 0; i < self.items.count; i++) {
//添加子控件
TBCuntomButton *button = [[TBCuntomButton alloc] init];
[self addSubview:button];
UITabBarItem *tabBarItem = self.items[i];
button.tag = i;
[button setImage:tabBarItem.image title:@"" edgeInsetsStyle:TPButtonEdgeInsetsStyleLeft imageTitleSpace:10 forState:UIControlStateNormal];
[button setImage:tabBarItem.selectedImage title:tabBarItem.title edgeInsetsStyle:TPButtonEdgeInsetsStyleLeft imageTitleSpace:10 forState:UIControlStateSelected];
[button setTitleColor:Color_FFFFFF forState:UIControlStateSelected];
button.titleLabel.font = JiangXiZhuoKaiFont(12);
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
if (i == 0) {
self.seletedButton = button;
[self buttonClick:button];
}
}
}
- (void) buttonClick:(UIButton *) button {
//1、取消上一个点击的状态
self.seletedButton.selected = NO;
// [self.seletedButton setTitleColor:Color_FFFFFF forState:UIControlStateNormal];
//2、选中当前点击按钮
button.selected = YES;
// [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
//3、记录当前选中按钮
self.seletedButton = button;
//4、通知外界切换子控制器
if ([self.delegate respondsToSelector:@selector(tabBar:index:)]) {
[self.delegate tabBar:self index:button.tag];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat buttonX = 0;
CGFloat buttonY = 0;
CGFloat buttonW = (_MainScreen_Width-38*2)/2;
CGFloat buttonH = 66;
int i = 0;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[TBCuntomButton class]]) {
button.frame = CGRectMake(buttonX+buttonW*i, buttonY, buttonW, buttonH);
i++;
}
}
}
@end
3、自定义继承UITabBarController的TabBarController
#import <UIKit/UIKit.h>
@class TBCustomTabBar;
NS_ASSUME_NONNULL_BEGIN
@interface SLTabBarViewController : UITabBarController
@property (nonatomic , strong) TBCustomTabBar *myTabBar;
@end
#import "SLTabBarViewController.h"
#import "BLMineViewController.h"
#import "TBTranslationViewController.h"
#import "TBCustomTabBar.h"
@interface SLTabBarViewController ()<TBCustomTabBarDelegate>
/***tabBar items 数组模型**/
@property (nonatomic , strong) NSMutableArray *items;
@end
@implementation SLTabBarViewController
- (NSMutableArray *)items {
if (!_items) {
_items = [NSMutableArray array];
}
return _items;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// [self tabBar:self.myTabBar index:0];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initialControllers];
//添加自定义的TabBar
[self setupTabBar];
}
-(void)viewDidLayoutSubviews{
[super viewWillLayoutSubviews];
self.myTabBar.frame = CGRectMake(38, 0, _MainScreen_Width-38*2, 66);
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
for (UIView *child in self.tabBar.subviews){
if ([child isKindOfClass:[UIControl class]]){
[child removeFromSuperview];
}
}
}
- (void) setupTabBar {
//添加自定义的tabbar
self.myTabBar = [[TBCustomTabBar alloc] init];
self.myTabBar.items = self.items;
self.myTabBar.delegate = self;
[self.tabBar addSubview:self.myTabBar];
self.myTabBar.backgroundColor = Color_Hex(@"#6779CB");
ViewBorderRadius(self.myTabBar, 33, 1.5, Color_333333);
[self tabBar:self.myTabBar index:0];
}
#pragma mark - 实现自定义tabBar代理
- (void)tabBar:(TBCustomTabBar *)tabBar index:(NSInteger)index {
//切换子控制器
[self setSelectedIndex:index];
}
//初始化子控制器
-(void)initialControllers {
[self setupController:[[TBTranslationViewController alloc]init] image:@"首页-填充 (1) 拷贝" selectedImage:@"组 7" title:@"首页"];
[self setupController:[[BLMineViewController alloc]init] image:@"我的 (10)" selectedImage:@"组 5(1)" title:@"我的"];
}
//设置控制器
-(void)setupController:(UIViewController *)childVc image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title {
UIImage *musicImage = [UIImage imageNamed:image];
UIImage *musicImageSel = [UIImage imageNamed:selectedImage];
musicImage = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
musicImageSel = [musicImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIViewController *viewVc = childVc;
viewVc.title=title;
viewVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:musicImage selectedImage:musicImageSel];
[self.items addObject:childVc.tabBarItem];
if (@available(iOS 15.0, *)) {
UITabBarAppearance *bar = [UITabBarAppearance new];
bar.backgroundColor = [UIColor clearColor];
bar.backgroundEffect = nil;
bar.stackedLayoutAppearance.selected.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:Color_NavBar};
bar.stackedLayoutAppearance.normal.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor grayColor]};
self.tabBar.scrollEdgeAppearance = bar;
self.tabBar.standardAppearance = bar;
} else {
self.tabBar.translucent = NO;
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:Color_NavBar} forState:UIControlStateSelected];
}
SLNavViewController *navi = [[SLNavViewController alloc]initWithRootViewController:viewVc];
[self addChildViewController:navi];
}
@end
到此实现了自己的项目需求,需要样式的时候可在自定义的View以及Button里进行UI实现。
网友评论