自定义底部tabbar的两种方式

第一种:利用系统自带的tabbarItem加一个自定义按钮:

#import "SZMTabBarController.h"
#import "SZMTabBar.h"
#import "SZMHomeViewCtrl.h"
#import "SZMNavigationController.h"
#import "SZMDiscoerViewCtrl.h"
@interface SZMTabBarController ()<SZMTabBarDelegate>

@end

@implementation SZMTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化一个自己的tabbar
    SZMTabBar *tabBar = [[SZMTabBar alloc]init];
    tabBar.delegate = self;
    //通过KVC去设置只读的属性
    [self setValue:tabBar forKey:@"tabBar"];
    
    //添加四个控制器
    //首页
    SZMHomeViewCtrl *homeCtrl = [[SZMHomeViewCtrl alloc]init];
    [self addChildVc:homeCtrl title:@"首页" imageName:@"tabbar_home" selImgName:@"tabbar_home_selected"];
    
    UITableViewController *messageCtrl = [[UITableViewController alloc]init];
    [self addChildVc:messageCtrl title:@"消息" imageName:@"tabbar_message_center" selImgName:@"tabbar_message_center_selected"];
    SZMDiscoerViewCtrl *discoveryCtrl = [[SZMDiscoerViewCtrl alloc]init];
    [self addChildVc:discoveryCtrl title:@"发现" imageName:@"tabbar_discover" selImgName:@"tabbar_discover_selected"];
    UITableViewController *profileCtrl = [[UITableViewController alloc]init];
    [self addChildVc:profileCtrl title:@"" imageName:@"tabbar_profile" selImgName:@"tabbar_profile_selected"];
    
}
//设置tabbar的一些属性
- (void)addChildVc:(UIViewController *)Controller title:(NSString *)title imageName:(NSString *)imgName selImgName:(NSString *)selImgName{
    Controller.title = title;
    Controller.tabBarItem.image = [UIImage imageNamed:imgName];
    Controller.tabBarItem.selectedImage = [UIImage imageNamed:selImgName];
    self.tabBar.tintColor = [UIColor orangeColor];
    SZMNavigationController *navCtrl = [[SZMNavigationController alloc]initWithRootViewController:Controller];
    
    [self addChildViewController:navCtrl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)TabBar:(SZMTabBar *)TabBar plusBtnDidClick:(UIButton *)btn{
    NSLog(@"1");
}

@end
#import <UIKit/UIKit.h>
@class SZMTabBar;
#warning 我们自定义的控件,如果是继承于系统的控件的话,而且系统的控件有代理,我们的协议一定要继承父类的协议
@protocol SZMTabBarDelegate <NSObject,UITabBarDelegate>

- (void)TabBar:(SZMTabBar *)TabBar plusBtnDidClick:(UIButton *)btn;

@end
@interface SZMTabBar : UITabBar

@property (nonatomic,weak) id<SZMTabBarDelegate> delegate;
@end
#import "SZMTabBar.h"

@interface SZMTabBar ()
@property (nonatomic,weak) UIButton *plusBtn;

@end
@implementation SZMTabBar
@dynamic delegate;
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //添加加号按钮
        UIButton *plusBtn = [[UIButton alloc]init];
        //给button设置不同状态下背景图片
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [self addSubview:plusBtn];
        self.plusBtn = plusBtn;
        
        plusBtn.size = plusBtn.currentBackgroundImage.size;
        //给按钮添加点击事件
        [plusBtn addTarget:self action:@selector(plusBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    return self;
}

- (void)plusBtnClick:(UIButton *)btn{

    //代理方法的实现
    if ([self.delegate respondsToSelector:@selector(TabBar:plusBtnDidClick:)]) {
        [self.delegate TabBar:self plusBtnDidClick:btn];
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    //调整加号按钮的位置
    self.plusBtn.centerX = self.width *0.5;
    self.plusBtn.centerY = self.height *0.5;
    
    //调整uitabbarbutton的大小和位置
    CGFloat tabBarBtnW = self.width *0.2;
    NSInteger index = 0;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            view.width = tabBarBtnW;
            view.x = index * tabBarBtnW;
           
            if (index == 1) {
                index++;
            }
             index++;
        }
    }
    
}
@end

第二种:自己完全自定义底部tabbar:

#import "SZMMainTabBarController.h"
#import "SZMBottomBarView.h"
@interface SZMMainTabBarController ()<SZMBottomBarViewDelegate>

@end

@implementation SZMMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];


    //加载子控制器
    [self loadSubControllers];

    //加载底部自定义view
    [self loadBottomView];
    
}


//加载底部自定义的view
- (void)loadBottomView{
    
    //创建底部的自定义的tabbarview
    SZMBottomBarView *BottomView = [[SZMBottomBarView alloc]init];
    BottomView.delegate = self;
    BottomView.backgroundColor = [UIColor redColor];
    
    BottomView.frame = self.tabBar.bounds;
    
    [self.tabBar addSubview:BottomView];

    
    //给自定义view里添加按钮
    
    for (int i = 0; i < self.viewControllers.count; i++) {                    
        
        NSString *NormalImgName = [NSString stringWithFormat:@"TabBar%d",i+1];
        NSString *SelectedImgName = [NSString stringWithFormat:@"TabBar%dSel",i+1];
        
        [BottomView addBarButtonWithNormalImgName:NormalImgName andSelectedImgName:SelectedImgName];
        
    }
    
    
    
    
}

- (void)SZMBottomBarView:(SZMBottomBarView *)SZMBottomBarViewWith didSclectedBtnIndex:(int)index{
    self.selectedIndex = index;
}
//加载五个子控制器到tabbar控制器
- (void)loadSubControllers{
    //购彩大厅
    UINavigationController *navHall = [self navigationControllerWithStoryboardName:@"Hall"];
    
    //竞技场
    UINavigationController *navArena = [self navigationControllerWithStoryboardName:@"Arena"];

    //发现
    UINavigationController *navDiscovery = [self navigationControllerWithStoryboardName:@"Discovery"];

    //开奖信息
    UINavigationController *navHistory = [self navigationControllerWithStoryboardName:@"History"];

    //我的彩票
    UINavigationController *navMyLottery = [self navigationControllerWithStoryboardName:@"MyLottery"];

    self.viewControllers = @[navHall,navArena,navDiscovery,navHistory,navMyLottery];
}

//加载对应的初始化控制器的方法
- (UINavigationController *)navigationControllerWithStoryboardName:(NSString *)storyboard{
    //1.创建stroyboard对象
    UIStoryboard *S1 = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    
    //2.实例化这个stroyboard中的初始化控制器
    UINavigationController *nav = [S1 instantiateInitialViewController];
    
    return nav;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
#import <UIKit/UIKit.h>
@class SZMBottomBarView;
@protocol SZMBottomBarViewDelegate <NSObject>
@optional
- (void)SZMBottomBarView:(SZMBottomBarView *)SZMBottomBarViewWith didSclectedBtnIndex:(int)index;

@end

@interface SZMBottomBarView : UIView

- (void)addBarButtonWithNormalImgName:(NSString *)NormalImgName andSelectedImgName:(NSString *)SelectedImgName;


@property (nonatomic,weak) id<SZMBottomBarViewDelegate> delegate;

@end
#import "SZMBottomBarView.h"
#import "SZMBottomBarBtn.h"

@interface SZMBottomBarView ()

@property (nonatomic,weak) UIButton *selectedBtn;

@end

@implementation SZMBottomBarView
- (void)addBarButtonWithNormalImgName:(NSString *)NormalImgName andSelectedImgName:(NSString *)SelectedImgName
{
    //创建一个uibutton
    SZMBottomBarBtn *btn = [[SZMBottomBarBtn alloc]init];
    
    //设置uibutton的背影图片
    UIImage *NorImg = [UIImage imageNamed:NormalImgName];
    UIImage *SelImg = [UIImage imageNamed:SelectedImgName];
    
    [btn setBackgroundImage:NorImg forState:UIControlStateNormal];
    [btn setBackgroundImage:SelImg forState:UIControlStateSelected];
    
    
    //把uibutton添加到自己身上
    [self addSubview:btn];
    
    //给每个底部的按钮添加点击事件
    [btn addTarget:self action:@selector(bottomBarButtonTouchDown:) forControlEvents:UIControlEventTouchDown];
    
}

//底部按钮点击事件的方法(占击哪个把哪个设置为选中状态)
- (void)bottomBarButtonTouchDown:(UIButton *)sennder
{
    //设置被点击按钮的状态
    self.selectedBtn.selected = NO;
    sennder.selected = YES;
    self.selectedBtn = sennder;
    
    //切换控制器
    if ([self.delegate respondsToSelector:@selector(SZMBottomBarView:didSclectedBtnIndex:)]) {
        [self.delegate SZMBottomBarView:self didSclectedBtnIndex:(int)sennder.tag];
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat y = 0;
    CGFloat w = self.bounds.size.width / self.subviews.count;
    CGFloat h = self.bounds.size.height;
    for (int i = 0; i < self.subviews.count; ++i) {
        CGFloat X = w *i;
        UIButton *button = (UIButton *)self.subviews[i];
        button.tag = i;
        button.frame = CGRectMake(X, y, w, h);
        //设置进入页面后第一个按钮为选中状态
        if (i == 0) {
            self.selectedBtn.selected = NO;
            button.selected = YES;
            self.selectedBtn = button;
        }
    }
    
    
    
}




@end
#import <UIKit/UIKit.h>

@interface SZMBottomBarBtn : UIButton

@end
#import "SZMBottomBarBtn.h"

@implementation SZMBottomBarBtn

- (void)setHighlighted:(BOOL)highlighted{
    
}
@end

 

转载于:https://www.cnblogs.com/ZMiOS/p/5080442.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值