在實際開發(fā)中有時遇見特殊的TabBar卑笨,系統(tǒng)已無法滿足,此時可自定義解決開發(fā)需求炼列。話不多說垮兑,直接上代碼巧娱,有問題歡迎指正。
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、取消上一個點擊的狀態(tài)
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 數(shù)組模型**/
@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 - 實現(xiàn)自定義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
到此實現(xiàn)了自己的項目需求柿估,需要樣式的時候可在自定義的View以及Button里進行UI實現(xiàn)。