自定义TabBar

//
//  CustomTabbar.h
//  自定制Tabbar
//
//  Created by qianfeng on 16/1/14.
//  Copyright (c) 2016年 Harcan. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomTabbar : UITabBarController

@end

 

//
//  CustomTabbar.m
//  自定制Tabbar
//
//  Created by qianfeng on 16/1/14.
//  Copyright (c) 2016年 Harcan. All rights reserved.
//

#import "CustomTabbar.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"

@interface CustomTabbar ()

@end

@implementation CustomTabbar

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self customTabbar];
    
}

//自定义tabbar
-(void)customTabbar
{
    //viewcontroller的字符串
    NSArray *VCs = @[@"ViewController1", @"ViewController2",
                     @"ViewController3", @"ViewController4"];
    
    //title
    NSArray *titles = @[@"值得买", @"发现", @"购物车", @"我的"];
    
    //默认图片
    NSArray *imageNames = @[@"mainNormal", @"goodsNormal",
                            @"dingyueItemNormal", @"personNormal"];
    
    //选择图片
    NSArray *selectedImageNames = @[@"mainSeleted", @"goodsseleted",
                                    @"dingyueItemSelected", @"personSeleted"];
    
    //tabbar.viewControllers
    NSMutableArray *viewControllers = [NSMutableArray new];
    
    //循环创建viewcontroller
    for (int i=0; i<VCs.count; i++) {
        
        //vc
        UIViewController *vc = [[NSClassFromString(VCs[i]) alloc] init];
        
        //nav
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        //nav.navigationBar.hidden = YES;
        
        //tabbarItem.title
        nav.tabBarItem.title = titles[i];
        
        //tabbarItem.image
        nav.tabBarItem.image = [[UIImage imageNamed:imageNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //tabbarItem.selectedImage
        nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //设置选择状态下的文字颜色
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
        
        //将nav添加到viewControllers
        [viewControllers addObject:nav];
        
    }
    
    //设置tabbar的viewControllers
    self.viewControllers = viewControllers;
    
    self.tabBar.translucent = NO; //tabbar的半透明度为NO(不透明)
}
@end

 

AppDelegate中设置 rootViewController 入口

//
//  AppDelegate.m
//  自定制Tabbar
//
//  Created by qianfeng on 16/1/14.
//  Copyright (c) 2016年 Harcan. All rights reserved.
//

#import "AppDelegate.h"
#import "CustomTabbar.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window.rootViewController = [[CustomTabbar alloc] init];
    
    return YES;
}

 

posted @ 2018-01-31 16:15  isHakan  阅读(148)  评论(0编辑  收藏  举报