我们在写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