先定义一个继承至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