iOSカスタムTabBar
3747 ワード
1、TabBarButton UIButton setHighlightedメソッドを継承し、ハイライト状態を削除し、必要に応じてButtonのフォント、フォント色、背景画像などをカスタマイズすることができます.
2、TabBarはUiviewを継承してTabBarを設定する
3、TabBarController
//
// TabBarItem.m
// TabBarDemo
//
// Created by iOS on 15/12/10.
// Copyright © 2015 iOS. All rights reserved.
//
#import "TabBarItem.h"
@implementation TabBarItem
- (void)setHighlighted:(BOOL)highlighted{
}
@end
2、TabBarはUiviewを継承してTabBarを設定する
//
// TabBar.m
// TabBarDemo
//
// Created by iOS on 15/12/10.
// Copyright © 2015 iOS. All rights reserved.
//
#import "TabBar.h"
#import "TabBarItem.h"
@interface TabBar()
@property(nonatomic,weak)UIButton *selectedBtn;
@end
@implementation TabBar
- (void)layoutSubviews{
[super layoutSubviews];
NSInteger count = self.subviews.count;
for (NSInteger i=0; i<count; i++) {
UIButton *btn = self.subviews[i];
btn.tag = i;
CGFloat x = i*self.frame.size.width/count;
CGFloat y = 0;
CGFloat w = self.frame.size.width/count;
CGFloat h = self.frame.size.height;
btn.frame = CGRectMake(x, y, w, h);
}
}
- (void)addButtonItem{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:btn];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
if (self.subviews.count==1) {
[self click:btn];
}
}
- (void)click:(UIButton *)btn{
self.selectedBtn.selected = NO;
btn.selected = YES;
self.selectedBtn = btn;
if ([self.delegate respondsToSelector:@selector(tabBar:selectedFrom:to:)]) {
[self.delegate tabBar:self selectedFrom:self.selectedBtn.tag to:btn.tag];
}
}
@end
3、TabBarController
//
// MainViewController.m
// TabBarDemo
//
// Created by iOS on 15/12/10.
// Copyright © 2015 iOS. All rights reserved.
//
#import "MainViewController.h"
#import "TabBar.h"
#import "TabBarItem.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
#import "SixViewController.h"
@interface MainViewController ()<TabBarDelegate>
@property(nonatomic, weak) UIButton *selectedBtn;
@property(nonatomic,weak)TabBar *tab;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = self.tabBar.bounds;
TabBar *tab =[[TabBar alloc]init];
tab.backgroundColor = [UIColor redColor];
tab.delegate = self;
tab.frame = rect;
[self.tabBar addSubview:tab];
self.tab = tab;
OneViewController *oneVC = [[OneViewController alloc]init];
[self setUpVieController:oneVC title:@"One"];
TwoViewController *twoVC = [[TwoViewController alloc]init];
[self setUpVieController:twoVC title:@"Two"];
ThreeViewController *threeVC = [[ThreeViewController alloc]init];
[self setUpVieController:threeVC title:@"ThreeVC"];
FourViewController *fourVC = [[FourViewController alloc]init];
[self setUpVieController:fourVC title:@"Four"];
FiveViewController *fiveVC = [[FiveViewController alloc]init];
[self setUpVieController:fiveVC title:@"FiveVC"];
}
- (void)setUpVieController:(UIViewController *)vc title:(NSString *)title{
vc.title = title;
[self addChildViewController:vc];
[self.tab addButtonItem];
}
- (void)tabBar:(TabBar *)tabBar selectedFrom:(NSInteger)from to:(NSInteger)to{
}
@end