RakuyoKit / RKOTabBarManage

使用UIView自定义TabBar。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RKOTabBarManage

粗略封装,使用UIView自定义TabBar。

提供以下功能:

  1. 中间的ExtraButton。
    • 如果不希望该btn在中间,删除/注释掉RKOTabBar.m文件中layoutSubviews方法下的如下代码就可以另该btn显示在末尾。
//        如果设置了额外的btn
       if (self.extraBtn) {
//            交换元素
           if (self.subviews.count == 3 && index == 1) {
               [self exchangeSubviewAtIndex:index withSubviewAtIndex:(index + 1)];

           } else if (self.subviews.count == 5 && index == 2) {
               [self exchangeSubviewAtIndex:index withSubviewAtIndex:(index + 2)];
               [self exchangeSubviewAtIndex:(index + 1) withSubviewAtIndex:(index + 2)];
           }
       }
  1. 可以设置TabBar上每一个button的图标和文字。并且当没有文字时图标居中显示
  2. 如果你想要,可以拿到每一个button,然后做点击事件。
  3. 可以设置TabBar的背景颜色或背景图片。
  4. push的时候隐藏TabBar,(暂时没有做到系统的效果)
  5. 可以自定义进入App后首先显示哪个控制器。

如何使用

  1. 下载完成后将RKOTabBar文件夹整个拖入您的项目。
  2. AppDelegate.m引入头文件#import "RKOTabBarController.h"后,在文件中编写如下代码:
    // 创建视图
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 创建测试用VC
    TestViewController *testVC = [[TestViewController alloc] init];
    TestViewController *testVC2 = [[TestViewController alloc] init];
    TestViewController *testVC3 = [[TestViewController alloc] init];
    TestViewController *testVC4 = [[TestViewController alloc] init];
    
    // 创建TabBar
    RKOTabBarController *tabBarVC = [RKOTabBarController sharedManager];
    
    // 设置TabBar的样式
    [tabBarVC tabBarTitleHighlightedColor:[UIColor blackColor] backgroundColor:[UIColor greenColor] backgroundImgName:nil];
    
    // 添加VC到TabBar中。
    [tabBarVC addViewController:testVC withTitle:@"test1" normalImageName:@"campus" selectImageName:@"person" selected:YES];
    [tabBarVC addViewController:testVC2 withTitle:@"test2" normalImageName:@"campus" selectImageName:@"person" selected:NO];
    [tabBarVC addViewController:testVC3 withTitle:@"test3" normalImageName:@"campus" selectImageName:@"person" selected:NO];
    [tabBarVC addViewController:testVC4 withTitle:@"test4" normalImageName:@"campus" selectImageName:@"person" selected:NO];
    
    // 设置根视图
    self.window.rootViewController = tabBarVC;
    
    // 显示窗口
    [self.window makeKeyAndVisible];

输入上述代码后我们就已经设置好了,运行App可以看到效果。

如果您想要添加中间自定义的button,还需要做以下操作:
3. 在将VC添加到TabBar的代码后面,输入以下代码

// 添加中间的btn
    UIButton *extraBtn = [[UIButton alloc] init];
    [tabBarVC addExtraBtn:extraBtn];
  1. 然后在您的控制器中遵从<RKOTabBarControllerDelegate>协议,如下设置代理:
[RKOTabBarController sharedManager].RKOTabBarControllerDelegate = self;
  1. 实现extraBtnClickedInTabBarController:方法,在该方法中直接输入您的点击事件的代码。(无需再添加点击事件)。

接口声明

RKOTabBarController.h

#import <UIKit/UIKit.h>

@class RKOTabBarController;

@protocol RKOTabBarControllerDelegate <NSObject>

/**
 代理方法,用于给extraBtn添加点击事件
 
 @param tabBarController TabBarController自身
 */
- (void)extraBtnClickedInTabBarController:(RKOTabBarController *)tabBarController;

@end


/**
 自定义TabBar的控制器
 
 本类负责暴露设置方法给外界,进而设置TabBar的各种属性。并且负责跳转逻辑。
 */
@interface RKOTabBarController : UITabBarController

// 设置delegate属性。
@property (nonatomic, weak) id<RKOTabBarControllerDelegate> RKOTabBarControllerDelegate;

/**
 单例方法
 
 @return RKOTabBar
 */
+ (instancetype)sharedManager;

/**
 将VC添加到TabBar中并设置对应的Btn的样式属性。

 @param viewController 被添加到TabBar的控制器。
 @param title 对应的TabBarButton标题。
 @param norImgName 普通状态下TabBarButton的图片。
 @param selImgName 被选中状态下TabBarButton的图片。
 @param selected 是否为初始进入的控制器。
 */
- (void)addViewController:(UIViewController *)viewController withTitle:(NSString *)title normalImageName:(NSString *)norImgName selectImageName:(NSString *)selImgName selected:(BOOL)selected;

/**
 设置TabBar样式的方法

 @param highligColor 被选中状态下TabBar标题文字的颜色。
 @param backgroundColor TabBar的背景颜色。
 @param imgName TabBar的背景图片。
 */
- (void)tabBarTitleHighlightedColor:(UIColor *)highligColor backgroundColor:(UIColor *)backgroundColor backgroundImgName:(NSString *)imgName;

/**
 隐藏TabBar
 */
- (void)hideTabView;

/**
 显示TabBar
 */
- (void)showTabView;

/**
 添加中间额外的button的方法

 @param btn 额外的button
 */
- (void)addExtraBtn:(UIButton *)btn;

@end

RKOTabBar.h

#import <UIKit/UIKit.h>

@class RKOTabBar;
@class RKOTabBarBtn;

/**
 RKOTabBarDelegate
 
 用于处理视图之间的跳转。
 
 */
@protocol RKOTabBarDelegate <NSObject>

/**
 代理方法,用以切换控制器。

 @param tabBar TabBar自身
 @param to 目标控制器的tag值
 */
- (void)tabBar:(RKOTabBar *)tabBar selectBtnFromCurrentTo:(NSInteger)to;

@end

/**
 RKOTabBar
 
 自定义TabBar。
 本类用于将ViewController自身的item属性添加到自定义TabBar中。
 以及设置包括背景图片、背景颜色、选中状态下的文字颜色、子视图的位置在内的TabBar自身属性。
 
 */
@interface RKOTabBar : UIView

// 额外的按钮。
@property (nonatomic, strong) UIButton *extraBtn;

// 背景图片。
@property (nonatomic, weak) UIImage *backgroundImg;
// 背景颜色
@property (nonatomic, strong) UIColor *backgroundColor;
// 文字颜色。
@property (nonatomic, strong) UIColor *titleColor;
// 是否刚进入就显示
@property (nonatomic, assign) BOOL selected;

// 设置delegate属性。
@property (nonatomic, weak) id<RKOTabBarDelegate> tabBarDelegate;

/**
 将VC的item信息添加到TabBar中。

 @param item 目标VC的item属性。
 */
- (void)addTabBarItem:(UITabBarItem *)item;

@end

RKOTabBarBtn.h

#import <UIKit/UIKit.h>

/**
 RKOTabBarBtn
 
 RKOTabBar上的自定义Btn。
 本类只负责设置Btn的图片和文字,及图片和文字的布局。不负责设置整个Btn的Frame。
 
 */
@interface RKOTabBarBtn : UIButton

/**
 暴露item的set方法用于设置。

 @param item Btn的item属性。
 */
- (void)setItem:(UITabBarItem *)item;

@end

About

使用UIView自定义TabBar。

License:MIT License


Languages

Language:Objective-C 100.0%