iOS 一种很方便的构造TarBar

直接在TarBarController中操作,代码如下:

#import "DLTabBarController.h"
#import "ViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
#import "DLNavigationController.h"


#define kClassKey           @"rootVCClassString" // 控制器对应的字符串
#define kTitleKey           @"title"             // title可以当做tabBarItem的title属性
#define kImageKey           @"imageName"         // image显示
#define kSelImageKey        @"selectedImageName" // 被选中后的image显示
#define kNavigationTitleKey @"navigationItemTitle" // navigationItem的title属性


@interface DLTabBarController ()

@end

@implementation DLTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 构造数组(为了防止控制器写错,应该现在外面写好后再复制粘贴进去)
    NSArray *childItemsArray = @[
                                 @{
                                     kClassKey           : @"ViewController",
                                     kTitleKey           : @"首页",
                                     kImageKey           : @"home",
                                     kSelImageKey        : @"home_select",
                                     kNavigationTitleKey : @"首页"
                                     },
                                 @{
                                     kClassKey           : @"TwoViewController",
                                     kTitleKey           : @"分类",
                                     kImageKey           : @"category",
                                     kSelImageKey        : @"category_select",
                                     kNavigationTitleKey : @"分类"
                                     },
                                 @{
                                     kClassKey           : @"ThreeViewController",
                                     kTitleKey           : @"",
                                     kImageKey           : @"center",
                                     kSelImageKey        : @"center_select",
                                     kNavigationTitleKey : @"home"
                                     },
                                 @{
                                     kClassKey           : @"FourViewController",
                                     kTitleKey           : @"购物车",
                                     kImageKey           : @"cart",
                                     kSelImageKey        : @"cart_select",
                                     kNavigationTitleKey : @"购物车"
                                     },
                                 @{
                                     kClassKey           : @"FiveViewController",
                                     kTitleKey           : @"",
                                     kImageKey           : @"mine",
                                     kSelImageKey        : @"mine_select",
                                     kNavigationTitleKey : @""
                                     }
                                 ];
    
    // 遍历字典得到对应的属性
    [childItemsArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIViewController *vc = [[NSClassFromString(dict[kClassKey]) alloc] init];
        vc.navigationItem.title = dict[kNavigationTitleKey];
        vc.tabBarItem.title = dict[kTitleKey];
        
        DLNavigationController *nav = [[DLNavigationController alloc] initWithRootViewController:vc];
        
        vc.tabBarItem.image = [[UIImage imageNamed:dict[kImageKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.selectedImage = [[UIImage imageNamed:dict[kSelImageKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        [self addChildViewController:nav];
    }];
    
}

本人是很喜欢这种做法的,虽然在写 key 对应的值的时候可能出错。但是对于后面的操作我们可以省去不少的代码。所以我在项目中基本都用户这个操作

posted on 2016-05-26 15:17  村里竹竿  阅读(330)  评论(0编辑  收藏  举报