- 首先需要创建一个自定义的tabBar的类,继承于UIview:实现自定义创建tabBar的item按钮,自定义tabBarDelegate,回调block等,具体代码如下:
// YDWTabBar.h
// YDWLiveShow
//
// Created by cptech on 2017/9/6.
// Copyright © 2017年 CPTECH_ydw. All rights reserved.
//
#import <UIKit/UIKit.h>
@class YDWTabBar;
// item枚举
typedef NS_ENUM(NSInteger, itemType) {
itemTypeLive = 100, // 直播界面
itemTypeMe, // 个人中心
itemTypeLanuch = 10 // 启动直播
};
// tabBar协议
@protocol YDWTabBarDelegate <NSObject>
- (void)tabBar:(YDWTabBar *)tabBar selectItem:(NSInteger)idx;
@end
// tabBar回调block
typedef void(^tabBarBlock)(YDWTabBar *tabBar, itemType idx);
@interface YDWTabBar : UIView
@property (nonatomic, weak) id<YDWTabBarDelegate> delegate;
@property (nonatomic, copy) tabBarBlock block;
@end
// YDWTabBar.m
// YDWLiveShow
//
// Created by cptech on 2017/9/6.
// Copyright © 2017年 CPTECH_ydw. All rights reserved.
//
#import "YDWTabBar.h"
@interface YDWTabBar ()
@property (nonatomic, strong) UIImageView *tabBarBgView; // tabBar背景
@property (nonatomic, copy) NSArray *itemsArray; // item图片数组
@property (nonatomic, strong) UIButton *selectItem; // tabBar按钮
@property (nonatomic, strong) UIButton *liveButton; // 直播按钮
@end
@implementation YDWTabBar
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat width = self.bounds.size.width/self.itemsArray.count;
for (int i = 0; i < [self subviews].count; i++) {
UIView *view = [self subviews][i];
if ([view isKindOfClass:[UIButton class]]) {
view.frame = CGRectMake((view.tag-itemTypeLive)*width, 0, width, self.frame.size.height);
}
}
[self.liveButton sizeToFit];
self.liveButton.center = CGPointMake(self.center.x, self.bounds.size.height-50);
[self bringSubviewToFront:self.liveButton];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.tabBarBgView];
[self addItemButton];
[self addSubview:self.liveButton];
}
return self;
}
- (void)addItemButton {
for (int i = 0; i < self.itemsArray.count; i++) {
UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
itemButton.tag = itemTypeLive+i;
[itemButton setTitle:self.itemsArray[i][@"title"] forState:UIControlStateNormal];
[itemButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[itemButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[itemButton.titleLabel setFont:[UIFont systemFontOfSize:11]];
[itemButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
itemButton.adjustsImageWhenHighlighted = NO; // 禁止图片在高亮状态下改变
[itemButton setImage:[UIImage imageNamed:self.itemsArray[i][@"image"]] forState:UIControlStateNormal];
[itemButton setImage:[UIImage imageNamed:[self.itemsArray[i][@"image"] stringByAppendingString:@"_p"]] forState:UIControlStateSelected];
[itemButton addTarget:self action:@selector(responderToItemSelected:) forControlEvents:UIControlEventTouchUpInside];
CGSize imageSize = itemButton.imageView.frame.size;
CGSize titleSize = itemButton.titleLabel.frame.size;
itemButton.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, imageSize.height-37, 20);
itemButton.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height-5, 0, 0, -titleSize.width);
[self addSubview:itemButton];
if (i == 0) {
itemButton.selected = YES;
self.selectItem = itemButton;
}
}
}
#pragma mark - Events Responder
- (void)responderToItemSelected:(UIButton *)sender {
// 响应tabBar协议的方法就回调
if ([self.delegate respondsToSelector:@selector(tabBar:selectItem:)]) {
[self.delegate tabBar:self selectItem:sender.tag];
}
// !self.block?:self.block(self, sender.tag);
if (self.block) {
self.block(self, sender.tag);
}
if (sender.tag == itemTypeLanuch) {
return;
}
self.selectItem.selected = NO;
sender.selected = YES;
self.selectItem = sender;
// item添加动画
[UIView animateWithDuration:0.2 animations:^{
sender.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
sender.transform = CGAffineTransformIdentity;
}];
}];
}
#pragma mark - Setter/Getter
- (UIImageView *)tabBarBgView {
if (!_tabBarBgView) {
_tabBarBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"global_tab_bg"]];
}
return _tabBarBgView;
}
- (NSArray *)itemsArray {
if (!_itemsArray) {
_itemsArray = @[@{@"image":@"tab_live",@"title":@"直播"},
@{@"image":@"tab_me", @"title":@"我的"}];
}
return _itemsArray;
}
- (UIButton *)liveButton {
if (!_liveButton) {
_liveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_liveButton setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal];
_liveButton.tag = itemTypeLanuch;
[_liveButton addTarget:self action:@selector(responderToItemSelected:) forControlEvents:UIControlEventTouchUpInside];
}
return _liveButton;
}
@end
- 其次,创一个tabBar的控制器,继承于UITabBarController:主要是装载生成自定义的tabBar,并配置界面视图控制器,代码如下:(当然,这其中,需要创建切换tabBar而展示的不同ViewControlles,我创建了“YDWLiveShowViewController”和“YDWMeCenterViewController”两个视图控制器界面,最好是继承于一个BaseViewController,方便以后修改导航栏和tabBar,还需要创建一个YDWNavViewController,继承于UINavigationController)
// YDWTabBarViewController.h
// YDWLiveShow
//
// Created by cptech on 2017/9/6.
// Copyright © 2017年 CPTECH_ydw. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface YDWTabBarViewController : UITabBarController
@end
// YDWTabBarViewController.m
// YDWLiveShow
//
// Created by cptech on 2017/9/6.
// Copyright © 2017年 CPTECH_ydw. All rights reserved.
//
#import "YDWTabBarViewController.h"
#import "YDWTabBar.h"
#import "YDWNavViewController.h"
#import "YDWLaunchLiveViewController.h"
@interface YDWTabBarViewController ()<YDWTabBarDelegate>
@property (nonatomic, strong) YDWTabBar *ydwTabBar; // 自定义的tabBar
@end
@implementation YDWTabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configViewControllers];
[self initilizeFace];
}
#pragma mark - Private Methods
- (void)configViewControllers {
NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"YDWLiveShowViewController",@"YDWMeCenterViewController"]];
for (int i = 0; i < array.count; i++) {
UIViewController *vc = [[NSClassFromString(array[i]) alloc] init];
YDWNavViewController *nav = [[YDWNavViewController alloc] initWithRootViewController:vc];
[array replaceObjectAtIndex:i withObject:nav];
}
self.viewControllers = array;
}
- (void)initilizeFace {
[self.tabBar addSubview:self.ydwTabBar];
// 去除tabBar的阴影线
[[UITabBar appearance] setShadowImage:[UIImage new]];
[[UITabBar appearance] setBackgroundImage:[UIImage new]];
}
#pragma mark - YDWTabBarDelegate
- (void)tabBar:(YDWTabBar *)tabBar selectItem:(NSInteger)idx {
if (idx != itemTypeLanuch) {
self.selectedIndex = idx - itemTypeLive;
return;
}
YDWLaunchLiveViewController *launVC = [[YDWLaunchLiveViewController alloc] init];
[self presentViewController:launVC animated:YES completion:nil];
}
#pragma mark - Setter/Getter
- (YDWTabBar *)ydwTabBar {
if (!_ydwTabBar) {
_ydwTabBar = [[YDWTabBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)];
_ydwTabBar.delegate = self;
}
return _ydwTabBar;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
- 最后在AppDelegate中初始化tabBarController即可:
YDWTabBarViewController *vc = [[YDWTabBarViewController alloc] init];
self.window.rootViewController = vc;