1 - (void)viewDidLoad
2 {
3 @autoreleasepool
4 { 6 NSArray *array = @[
7
8 [[UIViewController alloc]init],
9 @"toolbar_talk",
10 @"XX",
11 @"toolbar_talk_hover",
12
13 [[UIViewController alloc] init],
14 @"toolbar_contacts",
15 @"XX",
16 @"toolbar_contacts_hover",
17
18 XX,
19 @"toolbar_albums",
20 @"XX",
21 @"toolbar_albums_hover",
22
23 [[UIViewController alloc] init],
24 @"toolbar_setting",
25 @"XX",
26 @"toolbar_setting_hover"
27 ];
28
29 self.iosUnder5 = [UIDevice currentDevice].systemVersion.doubleValue < 5.0;
30
31 //选择卡的样式
32 NSDictionary *attribute = nil;
33 //选择时样式
34 NSDictionary *selectedAttribute = nil;
35 //选择时颜色
36 UIColor *selectedColor = [UIColor colorWithUInt:0x4e88c2];
37 //正常时颜色
38 UIColor *textColor = [UIColor colorWithUInt:0x6f819b];
39
40 CGRect rect = self.tabBar.bounds;
41 rect.size.width = rect.size.width * 3 / array.count;
42
43 //选卡样背景
44 if(self.iosUnder5)
45 {
46 self.tabBar.layer.contents = (id)[UIImage imageWithColor:[UIColor colorWithUInt:0xf5f5f5]];
47 }
48 else
49 {
50 UIImage *image = [UIImage imageWithColor:[UIColor colorWithUInt:0xf5f5f5]];
51 self.tabBar.backgroundImage = image;
52
53 image = [UIImage imageWithColor:[UIColor clearColor]];
54 self.tabBar.selectionIndicatorImage = image;
55
56 attribute = @{UITextAttributeTextColor : textColor };
57 selectedAttribute = @{UITextAttributeTextColor : selectedColor};
58 }
59
60 NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count / 3];
61
62 for(NSInteger i = 0; i < array.count; i += 4)
63 {
64 UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:array[i]];
65 [nav.navigationBar wojiaStyle];
66 items[items.count] = nav;
67
68 UIImage *image = [UIImage imageWithResource:array[i + 1]];
69 UIImage *selectedImage = [UIImage imageWithResource:array[i + 3]];
70
71 if(self.iosUnder5)
72 {
73 nav.tabBarItem.enabled = NO;
74 nav.tabBarItem.image = nil;
75 UIButton *button = [[UIButton alloc]initWithFrame:rect];
76 rect.origin.x += rect.size.width;
77 button.tag = i / 3 + kBarButtonStartTag;
78 button.selected = YES;
79 button.adjustsImageWhenHighlighted = NO;
80 button.titleLabel.font = FontWithSize(10);
81 button.titleLabel.shadowOffset = CGSizeMake(1, 1);
82 [button setTitle:array[i + 2]
83 forState:UIControlStateNormal];
84 [button setTitleColor:[UIColor colorWithUInt:0x6f8f9b]
85 forState:UIControlStateNormal];
86 [button setImage:image
87 forState:UIControlStateSelected];
88 [button setImage:selectedImage
89 forState:UIControlStateNormal];
90 [button setTitleColor:textColor
91 forState:UIControlStateSelected];
92 [button setTitleColor:selectedColor
93 forState:UIControlStateNormal];
94 //绑定事件
95 [button addTarget:self
96 action:@selector(tabBarButtonWillSelected:)
97 forControlEvents:UIControlEventTouchDown];
98 [self.tabBar addSubview:button];
99
100 //文本尺寸
101 CGSize textSize = [button.currentTitle sizeWithFont:button.titleLabel.font];
102 button.imageEdgeInsets = UIEdgeInsetsMake(-6, textSize.width, 0, 0);
103 button.titleEdgeInsets = UIEdgeInsetsMake(image.size.height + 3, -image.size.width , 0, 0);
104
105 }
106 else
107 {
108 nav.tabBarItem.image = image;
109 nav.tabBarItem.title = array[i + 2];
110 nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -2);
111 [nav.tabBarItem setFinishedSelectedImage:selectedImage
112 withFinishedUnselectedImage:image];
113 [nav.tabBarItem setTitleTextAttributes:attribute
114 forState:UIControlStateNormal];
115 [nav.tabBarItem setTitleTextAttributes:selectedAttribute
116 forState:UIControlStateSelected];
117 #ifdef __IPHONE_7_0
118 nav.tabBarItem.selectedImage = selectedImage;
119 #endif
120 }
121 }
122
123 self.viewControllers = items;
124
125
126 self.selectedIndex = 0;
127
128 [super viewDidLoad];
129 }