iOS自定义tabar 的使用

@interface MTTabButton :UIButton

@end

@implementation MTTabButton

//取消高亮状态

- (void)setHighlighted:(BOOL)highlighted

{

}

@end

@interface MainController ()

@property(weak,nonatomic)UIButton *selectBtn;

@property(nonatomic,strong)MTtabar * custabar;

@end

@implementation MainController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor =[UIColor whiteColor];

[self addtabar];

[self addbtn];

[self addChileVC];

}

-(void)addtabar{

MTtabar * custabar =[[MTtabar alloc]init];

custabar.frame = self.tabBar.frame;

[self.view addSubview:custabar];

self.custabar = custabar;

}

-(void)addbtn{

for (int i = 0; i<3; i++) {

MTTabButton * btn =[[MTTabButton alloc]init];

NSString *normalImage = [NSString stringWithFormat:@"TabBar%@",@(i + 1)];

NSString *selectImage = [NSString stringWithFormat:@"TabBar%@Sel",@(i + 1)];

btn.tag = i;

[btn setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];

[btn setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];

CGFloat btnW = self.view.frame.size.width / 3;

CGFloat btnH = 49;

btn.frame = CGRectMake(i*btnW, 0, btnW, btnH);

if (i == 0) {

btn.selected =YES;

self.selectBtn = btn;

}

[btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchDown];

[self.custabar addSubview:btn];

}

}

-(void)btnclick:(UIButton * )sender{

self.selectBtn.selected = NO;

sender.selected = YES;

self.selectBtn = sender;

self.selectedIndex = sender.tag;

}

-(void)addChileVC{

ThreeViewController * threeVC =[[ThreeViewController alloc]init];

UINavigationController * three =[self boardNameInstantVc:threeVC];

OtherViewController * otherVC =[[OtherViewController alloc]init];

UINavigationController * other =[self boardNameInstantVc:otherVC];

ElseController * elseVC =[[ElseController alloc]init];

UINavigationController * elses =[self boardNameInstantVc:elseVC];

self.viewControllers = @[three,other,elses];

}

- (UINavigationController *)boardNameInstantVc:(UIViewController *)controller{

UINavigationController * nav =[[UINavigationController alloc]initWithRootViewController:controller];

return nav;

}

这就是所有的代码!

你可能感兴趣的:(iOS自定义tabar 的使用)