参照常用的app中的tabbar进行了大致的封装,效果图如下

 

废话不说,贴代码

该项目中主要使用了自定义的tabBar以及tabBarController

【1】自定义tabBar

#import <UIKit/UIKit.h>

@protocol CenterTabBarDelegate<NSObject>

- (void)centerTabBarClick;

@end

@interface CenterTabBar : UITabBar


/*添加中间按钮
 image-图片
 centerColor-中心按钮背景颜色 nil则默认红色
 clickHandler-按钮点击事件
 */
- (void)addCenterBtnWithImage:(NSString *)image
                  centerColor:(UIColor *)centerColor;

@property (weak, nonatomic) id <CenterTabBarDelegate> centerDelegate;

@end

 

#import "CenterTabBar.h"

@interface CenterTabBar ()

@property (nonatomic, weak) UIButton *centerBtn;
@property (nonatomic, assign) BOOL createCenter;

@end

@implementation CenterTabBar

- (void)addCenterBtnWithImage:(NSString *)image
                  centerColor:(UIColor *)centerColor {
    self.createCenter = YES;
    
    UIButton *centerBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    centerBtn.adjustsImageWhenHighlighted = NO;
    centerBtn.clipsToBounds = YES;
    self.centerBtn = centerBtn;
    [self addSubview:centerBtn];
    
    self.centerBtn.backgroundColor = centerColor ? centerColor : [UIColor redColor];
    [self.centerBtn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [self.centerBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnClick {
    if (self.centerDelegate && [self.centerDelegate respondsToSelector:@selector(centerTabBarClick)]) {
        [self.centerDelegate centerTabBarClick];
    }

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.createCenter) {
        NSInteger totalNum = self.items.count + 1;
        CGFloat totalWidth = self.bounds.size.width;
        CGFloat totalHeight = self.bounds.size.height;

        //设置中间按钮的中心点
        CGPoint tempPoint = self.centerBtn.center;
        tempPoint.x = totalWidth * 0.5;//水平居中
        tempPoint.y = totalHeight * 0.3;//垂直方向中心点设置
        self.centerBtn.center = tempPoint;
        
        CGFloat tabBarButtonW = totalWidth / totalNum;
        
        //设置中间按钮的大小及圆角
        self.centerBtn.layer.cornerRadius = MIN(tabBarButtonW, totalHeight)/2;
        CGRect centerRect = self.centerBtn.frame;
        centerRect.size.width = MIN(tabBarButtonW, totalHeight);
        centerRect.size.height = MIN(tabBarButtonW, totalHeight);
        self.centerBtn.frame = centerRect;
        
        //设置其他按钮的位置
        CGFloat tabBarButtonIndex = 0;
        for (UIView *child in self.subviews) {
            Class class = NSClassFromString(@"UITabBarButton");
            if ([child isKindOfClass:class]) {
                
                CGRect tempRect = child.frame;
                tempRect.origin.x = tabBarButtonIndex * tabBarButtonW;
                tempRect.size.width = tabBarButtonW;
                child.frame = tempRect;

                // 增加索引
                tabBarButtonIndex++;
                if (tabBarButtonIndex == (self.items.count / 2)) {
                    tabBarButtonIndex++;
                }
            }
        }
        
    }

}



@end

 

【2】自定义tabBarController

#import <UIKit/UIKit.h>

@protocol BaseTabBarControllerDelegate <NSObject>

- (void)centerBtnClick;

@end

@interface BaseTabBarController : UITabBarController

/*初始化
 normalColor-TabBar默认颜色 nil则默认黑色
 selectedColor-TabBar选中颜色 nil则默认红色
 */
+ (instancetype)instanceWithNormalColor:(UIColor *)normalColor
                          selectedColor:(UIColor *)selectedColor;


/*TabBar默认颜色*/
@property (nonatomic, strong)UIColor *normalColor;

/*TabBar选中颜色*/
@property (nonatomic, strong)UIColor *selectedColor;


@property (weak, nonatomic) id <BaseTabBarControllerDelegate> centerDelegate;


/*添加子控制器对应TabBar
 childVC-子控制器
 title-子控制器标题,需注意当控制器设置了title时会使用控制器self.title而不是这时候传入的title
 image-图片
 selectedImage-选中图片
 */
- (void)addChildVc:(UIViewController *)childVC title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage;



/*添加中间按钮
 image-图片
 centerColor-中心按钮背景颜色 nil则默认红色
 clickHandler-按钮点击事件
 */
- (void)addCenterBtnWithImage:(NSString *)image
                  centerColor:(UIColor *)centerColor;



@end

 

#import "BaseTabBarController.h"
#import "FourViewController.h"
#import "CenterTabBar.h"

@interface BaseTabBarController ()<CenterTabBarDelegate>

@property (weak, nonatomic) CenterTabBar *centerTabBar;

@end

@implementation BaseTabBarController
#pragma mark - lifeCycle
+ (instancetype)instanceWithNormalColor:(UIColor *)normalColor
                          selectedColor:(UIColor *)selectedColor {
    BaseTabBarController *tabBarController = [[BaseTabBarController alloc]init];
    tabBarController.normalColor = normalColor ? normalColor : [UIColor blackColor];
    tabBarController.selectedColor = selectedColor ? selectedColor : [UIColor redColor];
    return tabBarController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    CenterTabBar *tabBar = [[CenterTabBar alloc] init];
    tabBar.centerDelegate = self;
    
    // KVC:如果要修系统的某些属性,但被设为readOnly,就是用KVC,即setValue:forKey:
    [self setValue:tabBar forKey:@"tabBar"];
    self.centerTabBar = tabBar;
}

- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title;
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = self.normalColor;
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = self.selectedColor;
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 先给外面传进来的小控制器 包装 一个导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];
    // 添加为子控制器
    [self addChildViewController:nav];
    
}

//添加中心按钮
- (void)addCenterBtnWithImage:(NSString *)image
                  centerColor:(UIColor *)centerColor {
    [self.centerTabBar addCenterBtnWithImage:image centerColor:centerColor];

}

#pragma mark - CenterTabBarDelegate
- (void)centerTabBarClick {
    if (self.centerDelegate && [self.centerDelegate respondsToSelector:@selector(centerBtnClick)]) {
        [self.centerDelegate centerBtnClick];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end

 【3】实际使用(appdelegate)

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OneViewController *oneVC = [board instantiateViewControllerWithIdentifier:@"OneViewController"];
    TwoViewController *twoVC = [board instantiateViewControllerWithIdentifier:@"TwoViewController"];
    ThreeViewController *threeVC = [board instantiateViewControllerWithIdentifier:@"ThreeViewController"];
    FourViewController *fourVC = [[FourViewController alloc]init];
    
    
    BaseTabBarController *tabBarController = [BaseTabBarController instanceWithNormalColor:nil selectedColor:[UIColor orangeColor]];
    tabBarController.centerDelegate = self;
    self.tabBarController = tabBarController;
    
    //添加tabbar的指向控制器
    [tabBarController addChildVc:oneVC title:@"one" image:@"SOSMenuBusinessCard" selectedImage:@"SOSMenuBusinessCard_Highlighted"];
    [tabBarController addChildVc:twoVC title:@"two" image:@"SOSMenuMoreFuncrions" selectedImage:@"SOSMenuMoreFuncrions_Highlighted"];
    [tabBarController addChildVc:threeVC title:@"three" image:@"SOSMenuSetting" selectedImage:@
     "SOSMenuSetting_Highlighted"];
    [tabBarController addChildVc:fourVC title:@"four" image:@"SOSMenuCommonlyUsedFunctions" selectedImage:@"SOSMenuCommonlyUsedFunctions_Highlighted"];
    
    //添加中心按钮
    [tabBarController addCenterBtnWithImage:@"SOSMenuLogout" centerColor:[UIColor orangeColor]];
    
    
    self.window.rootViewController = tabBarController;    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)centerBtnClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"点击了中心tabbar"  message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
    [self.tabBarController presentViewController:alert animated:YES completion:nil];

}

 

 其中,你可选择添加哪些控制器作为tabbar的指向控制器,另中心按钮可以在需要时添加,不需要时可以不添加,按自己项目需求来调用对应方法。

 

posted on 2017-04-26 14:18  fatal-奚山遇白  阅读(201)  评论(0编辑  收藏  举报