一个继承UITabbarController的基类,可以很方便的布局好tabbar的各个Item

我们在写UITabBarController的时候首先要做的是

1.它有几个子控制器。

2.它的item要显示的图片和字,以及选中的时候item显示的图片和字

每次我们写一个项目都会去配置它,写很多之前重复写的代码。现在这一个基类正好解决这个问题。

这里分为控制器全为storyboard写的和纯代码两种情况。

使用方法:1.继承它 2.在viewdidload中,写要配置的基本信息。

假如它有三个子控制器:

1.storyboard的情况:

/**storyboard写的VC*/

- (void)setTabbarController:(UIStoryboard *)sb withArray:(NSMutableArray *)tabbarArr;

1.我们继承基类后的这个tabbarcontroller要关联我们的storyboard创建的控制器。

2.在我们创建的tabbarcontroller代码viewdidLoad中调用以上的方法。

3.sb就是关联的storyboard  tabbarArr这个数组是用来配置item的一些信息

    NSMutableArray *arr2 =

    [NSMutableArrayarrayWithArray:@[

                      @{@"sbid":@"VC1",        《---这是我们storyboard创建的vc的storyboardID

                        @"img":@"tab_home_nor",  《--- 这是normal下item的图片

                        @"selectedimg":@"tab_home_sel",《---这是选中下item的图片

                        @"title":@"首页",《---这是item图片下显示的文字

                        @"normalColor":@"#818181",     《---这是item normal下的文字颜色  是十六进制

                        @"selectedColor":@"#0090e6"     《---这是选中下的文字颜色  

                        },                                                                          

                     ...

                      ]];

2.纯代码的情况:

/**纯代码写的VC*/

-(void)setCodeTabbarController:(NSMutableArray *)VCArr withArray:(NSMutableArray *)tabbarArr;

这方法是在我们继承基类后 调用的 第一个vcarr就是子控制器的数组  第二个tabbarArr就是item信息的数组

1.先初始化各个控制器 如:

  ViewController *vc1 = [[ViewControlleralloc] init];

  ViewController1 *vc2 = [[ViewController1alloc] init];

  ViewController2 *vc3 = [[ViewController2alloc] init];

然后加入到VCArr

2.配置各个item信息

 NSMutableArray *arr2 =

    [NSMutableArray arrayWithArray:@[

                      @{

                        @"img":@"tab_home_nor",  《--- 这是normal下item的图片

                        @"selectedimg":@"tab_home_sel",《---这是选中下item的图片

                        @"title":@"首页",《---这是item图片下显示的文字

                        @"normalColor":@"#818181",     《---这是item normal下的文字颜色  是十六进制

                        @"selectedColor":@"#0090e6"     《---这是选中下的文字颜色  

                        },                                                                          

                    ...

                      ]];

基类的源代码

#import <UIKit/UIKit.h>


@interface FKBaseTabBarController :UITabBarController


/**storyboard写的VC*/

- (void)setTabbarController:(UIStoryboard *)sb withArray:(NSMutableArray *)tabbarArr;


/**纯代码写的VC*/

-(void)setCodeTabbarController:(NSMutableArray *)VCArr withArray:(NSMutableArray *)tabbarArr;

@end


#import "FKBaseTabBarController.h"


@interface FKBaseTabBarController ()<UITabBarDelegate,UIGestureRecognizerDelegate>


@property (nonatomic,strong)NSMutableArray *FKviewControllers;

@end


@implementation FKBaseTabBarController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    // Do any additional setup after loading the view.

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



// 构造tabbarController的子视图控制器

- (void)setTabbarController:(UIStoryboard *)sb withArray:(NSMutableArray *)tabbarArr{

    

    _FKviewControllers = [NSMutableArrayarray];

    for (int i=0; i<tabbarArr.count; i++) {

        UINavigationController *nav = [sbinstantiateViewControllerWithIdentifier:tabbarArr[i][@"sbid"]];

      [selfsetImage:tabbarArr[i][@"img"]SelectImage:tabbarArr[i][@"selectedimg"]BarItem:nav title:tabbarArr[i][@"title"]normalTextColor:tabbarArr[i][@"normalColor"]SelectedTextColor:tabbarArr[i][@"selectedColor"]];

        [self.FKviewControllersaddObject:nav];

    }

    self.viewControllers =self.FKviewControllers;

}



- (void)setCodeTabbarController:(NSMutableArray *)VCArr withArray:(NSMutableArray *)tabbarArr{

    _FKviewControllers = [NSMutableArrayarray];

    for (int i=0; i<tabbarArr.count; i++) {

        [selfsetImage:tabbarArr[i][@"img"]SelectImage:tabbarArr[i][@"selectedimg"]BarItem:VCArr[i] title:tabbarArr[i][@"title"]normalTextColor:tabbarArr[i][@"normalColor"]SelectedTextColor:tabbarArr[i][@"selectedColor"]];

    }

    

    _FKviewControllers = VCArr;

    self.viewControllers =self.FKviewControllers;

}


// 设置选中与不选中的图片

- (void)setImage:(NSString *)imagestr SelectImage:(NSString *)selectImagestr BarItem:(UIViewController *)nav  title:(NSString *)title normalTextColor:(NSString *)normalColor SelectedTextColor:(NSString *)selectedColor{

    nav.tabBarItem = [[UITabBarItemalloc] initWithTitle:titleimage:[UIImageimageNamed:imagestr] selectedImage:[UIImageimageNamed:selectImagestr]];

    nav.tabBarItem.image = [nav.tabBarItem.imageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    nav.tabBarItem.selectedImage = [nav.tabBarItem.selectedImageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    [nav.tabBarItemsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[selfgetColor:selectedColor],NSForegroundColorAttributeName,[UIFontsystemFontOfSize:11],NSFontAttributeName,nil]forState:UIControlStateSelected];

    [nav.tabBarItemsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[selfgetColor:normalColor],NSForegroundColorAttributeName,[UIFontsystemFontOfSize:11],NSFontAttributeName,nil] forState:UIControlStateNormal];

    nav.tabBarItem.titlePositionAdjustment =UIOffsetMake(0, -4);

    nav.tabBarItem.imageInsets =UIEdgeInsetsMake(-2,0, 2,0);

}

//@"#0090e6" @"#818181"

- (UIColor *)getColor:(NSString *)color

{

    NSString *cString = [[colorstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]uppercaseString];

    

    // String should be 6 or 8 characters

    if ([cStringlength] < 6) {

        return [UIColorclearColor];

    }

    

    // strip 0X if it appears

    if ([cStringhasPrefix:@"0X"])

        cString = [cString substringFromIndex:2];

    if ([cStringhasPrefix:@"#"])

        cString = [cString substringFromIndex:1];

    if ([cStringlength] != 6)

        return [UIColorclearColor];

    

    // Separate into r, g, b substrings

    NSRange range;

    range.location =0;

    range.length =2;

    

    //r

    NSString *rString = [cStringsubstringWithRange:range];

    

    //g

    range.location =2;

    NSString *gString = [cStringsubstringWithRange:range];

    

    //b

    range.location =4;

    NSString *bString = [cStringsubstringWithRange:range];

    

    // Scan values

    unsignedint r, g, b;

    [[NSScannerscannerWithString:rString] scanHexInt:&r];

    [[NSScannerscannerWithString:gString] scanHexInt:&g];

    [[NSScannerscannerWithString:bString] scanHexInt:&b];

    

    return [UIColorcolorWithRed:((float) r /255.0f) green:((float) g /255.0f) blue:((float) b /255.0f) alpha:1.0f];

}

@end


如果您还有疑惑,请参考例子 https://github.com/frankkay/FKBaseTabBarController 或者私聊我哦
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值