CYLTabBarController的简单使用

 1 #pragma mark- 登录成功跳转至主页
 2 -(void)jumpToMainVC {
 3     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
 4 
 5     NSArray *normalImage = @[@"notice_icon_normal", @"news_icon_normal", @"contacts_icon_normal", @"application_icon_normal"];
 6     NSArray *selectImage = @[@"notice_icon_pressed", @"news_icon_pressed", @"contacts_icon_pressed", @"application_icon_pressed"];
 7     NSArray *vcClass = @[@"NotifyViewController", @"NewsViewController", @"ContactsViewController", @"ApplicationViewController"];
 8     NSArray *titleArray = @[@"通知", @"新闻", @"联系人", @"应用"];
 9     NSMutableArray *allArray = [NSMutableArray array];
10     
11     for (int i = 0; i < 4; i++) {
12         Class cla = NSClassFromString(vcClass[i]);
13         UIViewController *vc = [[cla alloc] init];
14         vc.navigationItem.title = titleArray[i];
15         [vc.tabBarItem setTitle:titleArray[i]];
16         [vc.tabBarItem setImage:[[UIImage imageNamed:normalImage[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
17         [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:selectImage[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
18         BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
19         [allArray addObject:nav];
20     }
21     UITabBarController *tab = [[UITabBarController alloc] init];
22     tab.viewControllers = allArray;
23     
24     //设置navigationBar样式
25     [self setUpNavigationBarAppearance];
26     //tabBarItem 的选中和不选中文字属性
27     [self setUpTabBarItemTextAttributes];
28     
29     self.window.rootViewController = tab;
30     
31     //打开收藏的数据库
32     [[CollectDataCenter shareInstance] openDataBase];
33 }
34 
35 
36 /**
37  *  设置navigationBar样式
38  */
39 - (void)setUpNavigationBarAppearance {
40     UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
41     UIImage *backgroundImage = nil;
42     NSDictionary *textAttributes = nil;
43     
44     if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
45         backgroundImage = [UIImage imageNamed:@"navigationBar_BG"];
46         
47         textAttributes = @{
48                            NSFontAttributeName: [UIFont boldSystemFontOfSize:18],
49                            NSForegroundColorAttributeName: [UIColor whiteColor],
50                            };
51     } else {
52 #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
53         backgroundImage = [UIImage imageNamed:@"navigationBar_BG"];
54         
55         textAttributes = @{
56                            UITextAttributeFont: [UIFont boldSystemFontOfSize:18],
57                            UITextAttributeTextColor: [UIColor blackColor],
58                            UITextAttributeTextShadowColor: [UIColor clearColor],
59                            UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],
60                            };
61 #endif
62     }
63     
64     [navigationBarAppearance setBackgroundImage:backgroundImage
65                                   forBarMetrics:UIBarMetricsDefault];
66     [navigationBarAppearance setTitleTextAttributes:textAttributes];
67 }
68 
69 /**
70  *  tabBarItem 的选中和不选中文字属性
71  */
72 - (void)setUpTabBarItemTextAttributes {
73     
74     // 普通状态下的文字属性
75     NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
76     normalAttrs[NSForegroundColorAttributeName] = RGBA(147, 147, 147, 1);
77     
78     // 选中状态下的文字属性
79     NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
80     selectedAttrs[NSForegroundColorAttributeName] = RGBA(105, 187, 42, 1);
81     
82     // 设置文字属性
83     UITabBarItem *tabBar = [UITabBarItem appearance];
84     [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
85     [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
86     
87     // 设置背景图片
88     UITabBar *tabBarAppearance = [UITabBar appearance];
89     [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbarBG"]];
90     //    tabBarAppearance.barTintColor = [UIColor lightGrayColor];
91 }

显示界面显示如下:



原文地址:https://www.cnblogs.com/pengsi/p/5295738.html