第一步:在AppDelegate中實現微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.rootViewController = [ZZTabViewController new]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; [self setupNavBar]; // Override point for customization after application launch. return YES; } - (void)setupNavBar { [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; UINavigationBar *bar = [UINavigationBar appearance]; CGFloat rgb = 0.1; bar.barTintColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:0.9]; bar.tintColor = [UIColor whiteColor]; bar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; }
第二步:自定義一個繼承於UINavigationController的ZZNavigationController類app
在自定義繼承於UITabBarController的ZZTabViewController中實現如下的代碼ide
#import "ZZTabViewController.h" #import "HomeViewController.h" #import "ContactViewController.h" #import "FindViewController.h" #import "MineViewController.h" #import "ZZNavigationController.h" #define kClassKey @"rootVCClassString" #define kTitleKey @"title" #define kImgKey @"imageName" #define kSelImgKey @"selectedImageName" #define Global_tintColor [UIColor colorWithRed:0 green:(190 / 255.0) blue:(12 / 255.0) alpha:1] @interface ZZTabViewController () @end @implementation ZZTabViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *childItemsArray = @[ @{kClassKey : @"HomeViewController", kTitleKey : @"微信", kImgKey : @"tabbar_mainframe", kSelImgKey : @"tabbar_mainframeHL"}, @{kClassKey : @"ContactViewController", kTitleKey : @"通信錄", kImgKey : @"tabbar_contacts", kSelImgKey : @"tabbar_contactsHL"}, @{kClassKey : @"FindViewController", kTitleKey : @"發現", kImgKey : @"tabbar_discover", kSelImgKey : @"tabbar_discoverHL"}, @{kClassKey : @"MineViewController", kTitleKey : @"個人", kImgKey : @"tabbar_me", kSelImgKey : @"tabbar_meHL"} ]; [childItemsArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) { UIViewController *vc = [NSClassFromString(dict[kClassKey]) new]; vc.title = dict[kTitleKey]; ZZNavigationController *nav = [[ZZNavigationController alloc] initWithRootViewController:vc]; UITabBarItem *item = nav.tabBarItem; item.title = dict[kTitleKey]; item.image = [UIImage imageNamed:dict[kImgKey]]; item.selectedImage = [[UIImage imageNamed:dict[kSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [item setTitleTextAttributes:@{NSForegroundColorAttributeName : Global_tintColor} forState:UIControlStateSelected]; [self addChildViewController:nav]; }]; // Do any additional setup after loading the view from its nib. } @end
運行效果如圖所示spa