iOS開發(fā)之自定義TabBar

在實(shí)際開發(fā)中有時(shí)遇見特殊的TabBar,系統(tǒng)已無法滿足,此時(shí)可自定義解決開發(fā)需求。話不多說,直接上代碼,有問題歡迎指正。
1、先自定義一個(gè)繼承于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、取消上一個(gè)點(diǎn)擊的狀態(tài)
    self.seletedButton.selected = NO;
//    [self.seletedButton setTitleColor:Color_FFFFFF forState:UIControlStateNormal];
    
    //2、選中當(dāng)前點(diǎn)擊按鈕
    button.selected = YES;
//    [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    
    //3、記錄當(dāng)前選中按鈕
    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 - 實(shí)現(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:@"我的"];
}

//設(shè)置控制器
-(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

到此實(shí)現(xiàn)了自己的項(xiàng)目需求,需要樣式的時(shí)候可在自定義的View以及Button里進(jìn)行UI實(shí)現(xiàn)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容