提取这个主要是因为,不想让 “AppDelegate.m” 里面有太多的代码,看起来乱。
步骤:
1. 创建一个分类,继承自 AppDelegate 。
2. 在类.h中写:
/** * 设置自定义的tabbar * * @return 自定义的tabBarController */ - (UIViewController *)setupCustomTabBarController;
3. 在类.m 中写:
@implementation AppDelegate (TabBar) /** * 设置自定义的tabbar * * @return 自定义的tabBarController */ - (UIViewController *)setupCustomTabBarController { // 第一个 UIViewController *firstViewController = [[UIViewController alloc] init]; firstViewController.view.backgroundColor = [UIColor redColor]; UIViewController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController]; // 第二个 UIViewController *secondViewController = [[UIViewController alloc] init]; secondViewController.view.backgroundColor = [UIColor orangeColor]; UIViewController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController]; // 第三个 UIViewController *thirdViewController = [[UIViewController alloc] init]; thirdViewController.view.backgroundColor = [UIColor greenColor]; UIViewController *thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController]; RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init]; [tabBarController setViewControllers:@[firstNavigationController, secondNavigationController, thirdNavigationController]]; [self customizeTabBarForController:tabBarController]; return tabBarController; } - (void)customizeTabBarForController:(RDVTabBarController *)tabBarController { UIImage *finishedImage = [UIImage imageNamed:@"tabbar_selected_background"]; UIImage *unfinishedImage = [UIImage imageNamed:@"tabbar_normal_background"]; NSArray *tabBarItemImages = @[@"first", @"second", @"third"]; NSInteger index = 0; for (RDVTabBarItem *item in [[tabBarController tabBar] items]) { [item setBackgroundSelectedImage:finishedImage withUnselectedImage:unfinishedImage]; UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected", [tabBarItemImages objectAtIndex:index]]]; UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal", [tabBarItemImages objectAtIndex:index]]]; [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage]; index++; } } - (void)customizeInterface { UINavigationBar *navigationBarAppearance = [UINavigationBar appearance]; UIImage *backgroundImage = nil; NSDictionary *textAttributes = nil; if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { backgroundImage = [UIImage imageNamed:@"navigationbar_background_tall"]; textAttributes = @{ NSFontAttributeName: [UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName: [UIColor blackColor], }; } else { #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0 backgroundImage = [UIImage imageNamed:@"navigationbar_background"]; textAttributes = @{ UITextAttributeFont: [UIFont boldSystemFontOfSize:18], UITextAttributeTextColor: [UIColor blackColor], UITextAttributeTextShadowColor: [UIColor clearColor], UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero], }; #endif } [navigationBarAppearance setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; [navigationBarAppearance setTitleTextAttributes:textAttributes]; }