AppDelegate.m
1 #import "AppDelegate.h" 2 #import "FirstViewController.h" 3 #import "SecondViewController.h" 4 #import "ThirdViewController.h" 5 #import "FourthViewController.h" 6 7 @interface AppDelegate () 8 9 @end 10 11 @implementation AppDelegate 12 13 14 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 15 // Override point for customization after application launch. 16 17 // 第一步:创建window 18 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 19 self.window.backgroundColor = [UIColor whiteColor]; 20 [self.window makeKeyAndVisible]; 21 22 23 24 // 第二步:创建UITabBarController对象 25 UITabBarController *mainTab = [[UITabBarController alloc] init]; 26 //mainTab.tabBar.barTintColor = [UIColor purpleColor]; 27 mainTab.tabBar.tintColor = [UIColor colorWithRed:15 / 255.0 green:178 / 255.0 blue:0 alpha:1]; 28 29 30 31 // 第三步:设置window的根控制器 32 self.window.rootViewController = mainTab; 33 34 35 36 // 第四步:设置UITabBarController的控制器数组 37 // 4.1 创建导航控制器并制定导航控制器的根视图 38 UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]]; 39 40 UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:[[SecondViewController alloc] init]]; 41 42 UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:[[ThirdViewController alloc] init]]; 43 44 UINavigationController *fourthNav = [[UINavigationController alloc] initWithRootViewController:[[FourthViewController alloc] init]]; 45 46 47 48 // 4.2 设置导航控制器的TabBarItem 49 UIImage *img1 = [UIImage imageNamed:@"tabbar_mainframe@3x"]; 50 UIImage *imgS1 = [[UIImage imageNamed:@"tabbar_mainframeHL@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 设置选中的图片保留图片原有的样式,不进行渲染 51 52 UIImage *img2 = [UIImage imageNamed:@"tabbar_contacts@3x"]; 53 UIImage *imgS2 = [[UIImage imageNamed:@"tabbar_contactsHL@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 54 55 UIImage *img3 = [UIImage imageNamed:@"tabbar_discover@3x"]; 56 UIImage *imgS3 = [[UIImage imageNamed:@"tabbar_discoverHL@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 57 58 UIImage *img4 = [UIImage imageNamed:@"tabbar_me@3x"]; 59 UIImage *imgS4 = [[UIImage imageNamed:@"tabbar_meHL@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 60 61 62 63 firstNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"微信" image:img1 selectedImage:imgS1]; 64 secondNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通讯录" image:img2 selectedImage:imgS2]; 65 thirdNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:img3 selectedImage:imgS3]; 66 fourthNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:img4 selectedImage:imgS4]; 67 68 69 // 4.3 将导航控制器对象添加到数组中 70 //[mainTab addChildViewController:firstNav]; 71 mainTab.viewControllers = @[firstNav, secondNav, thirdNav, fourthNav]; 72 73 74 #pragma mark - UIAppearance 75 76 // 一键设置 77 [UINavigationBar appearance].barTintColor = [UIColor blackColor]; 78 79 [UINavigationBar appearance].barStyle = UIBarStyleBlack; 80 81 [UINavigationBar appearance].tintColor = [UIColor whiteColor]; 82 83 84 // 同理,也有,但是只有一个tabbar,没必要而已 85 // [UITabBar appearance] 86 87 88 return YES; 89 } 90 91 92 @end
FirstViewController.m
1 #import "FirstViewController.h" 2 #import "Test1ViewController.h" 3 4 @interface FirstViewController () 5 6 @end 7 8 @implementation FirstViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 // Do any additional setup after loading the view. 13 14 15 self.title = @"微信"; 16 self.navigationController.navigationBar.barTintColor = [UIColor blackColor]; 17 18 // [self.navigationController.navigationBar setBackgroundImage:<#(nullable UIImage *)#> forBarMetrics:<#(UIBarMetrics)#>] 19 20 // 修改title的颜色和大小 21 // [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}]; 22 23 24 // 设置barstyle修改状态栏(简单,可以用) 25 // self.navigationController.navigationBar.barStyle = UIBarStyleBlack; 26 27 28 // 添加右按钮 29 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click:)]; 30 31 } 32 33 - (void)click:(UIBarButtonItem *)sender { 34 35 Test1ViewController *test1VC = [[Test1ViewController alloc] init]; 36 37 // 设置push隐藏tabbar 38 test1VC.hidesBottomBarWhenPushed = YES; 39 40 [self.navigationController pushViewController:test1VC animated:YES]; 41 } 42 43 @end