IOS开发UI:自定义TabBar

先定义一个继承至UITabBarController的类

UITabBarController.m的 内容为


由于系统UITabBar无法满足我们的需求所以我们自定义一个属于自己的UITabBar

这里用一个继承至UIView的类来实现我们的内容

CustMoBar.h 的内容

CustMoBar.m 的内容


实现效果


代码

#import "TabBarControll.h"
#import "CustMoBar.h"
@interface TabBarControll ()

@end

@implementation TabBarControll

- (void)viewDidLoad {
    
    
    // 自定义Bar
    CustMoBar *bar = [[CustMoBar alloc] initWithFrame:self.tabBar.bounds];
    // 添加按钮
    for (NSInteger index=0; index < 5; index++)
    {
        [bar addTabBtnFromTitleName:nil forIcon:@"Taric" forHigIcon:@"Taric"];
    }
    // 回调切换控制器
    [bar selectorIndex:^(NSInteger index) {
        self.selectedIndex = index;
    }];
    [self.tabBar addSubview:bar];
    [super viewDidLoad];

}

---------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------

#import <UIKit/UIKit.h>

typedef void(^Block)(NSInteger index);

@interface CustMoBar : UIView
/**
 *  添加按钮接口
 */
- (void)addTabBtnFromTitleName:(NSString*)title forIcon:(NSString*)icon forHigIcon:(NSString*)Higicon;

@property (nonatomic,copy)Block select;
/**
 * 回调选择控制界面
 */
- (void)selectorIndex:(Block)blocl;

@end

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#import "CustMoBar.h"

@interface CustMoBar ()
/**
 *  选中按钮
 */
@property (nonatomic,weak)UIButton * selectorBtn;
@end

@implementation CustMoBar
- (void)selectorIndex:(Block)blocl
{
    self.select = blocl;
}

- (void)addTabBtnFromTitleName:(NSString*)title forIcon:(NSString*)icon forHigIcon:(NSString*)Higicon
{
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.imageView.layer.cornerRadius = 20;
    btn.imageView.layer.masksToBounds = YES;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:Higicon] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(touchMenu:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}
- (void)touchMenu:(UIButton*)btn
{
    self.selectorBtn.selected = NO;
    btn.selected = YES;
    self.selectorBtn = btn;
    if (self.select)self.select(btn.tag);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    for (NSInteger index=0; index < self.subviews.count;index++)
    {
            UIButton* btn = self.subviews[index];
            btn.tag = index;
            CGFloat btnW = self.frame.size.width/self.subviews.count;
            CGFloat btnH = self.frame.size.height;
            CGFloat btnX = btnW * index;
            CGFloat btnY = 0;
            btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
            if (index == 0)[self touchMenu:btn];
    }
}
@end



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值