Second Chapter
創(chuàng)建程序的主架構(gòu)
- 大多數(shù)一般都使用tabBarController嵌套著navgationCotroller,系統(tǒng)默認(rèn)的tabBarItem如果不是我們需要的,要想修改,可以統(tǒng)一通過(guò)UIAppearance來(lái)設(shè)置.
- 只要方法名或者屬性名后面帶有UI_APPEARANCE_SELECTOR這個(gè)宏,就可以通過(guò)[XXX appearance]對(duì)象來(lái)統(tǒng)一設(shè)置
//設(shè)置普通狀態(tài)文字的狀態(tài)
NSMutableDictionary *normalAtts = [NSMutableDictionary dictionary];
normalAtts[NSForegroundColorAttributeName] = [UIColor grayColor];
normalAtts[NSFontAttributeName] = [UIFont systemFontOfSize:14];
//設(shè)置選中狀態(tài)文字的狀態(tài)
NSMutableDictionary *SelectedAtts = [NSMutableDictionary dictionary];
SelectedAtts[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
SelectedAtts[NSFontAttributeName] = [UIFont systemFontOfSize:14];
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:normalAtts forState:UIControlStateNormal];
[item setTitleTextAttributes:SelectedAtts forState:UIControlStateSelected];
- 添加子控制器
- (void)setupAllChildVc{
[self setupChildVc:[[CqNavigationViewController alloc] initWithRootViewController:[[CqEssenceViewController alloc] init]] title:@"" imageName:@"" selectedImage:@""];
[self setupChildVc:[[CqNavigationViewController alloc] initWithRootViewController:[[CqNewViewController alloc] init] ] title:@"" imageName:@"" selectedImage:@""];
[self setupChildVc:[[CqNavigationViewController alloc] initWithRootViewController:[[CqFocusViewController alloc] init] ] title:@"" imageName:@"" selectedImage:@""];
[self setupChildVc:[[CqNavigationViewController alloc] initWithRootViewController:[[CqMeViewController alloc] init] ]title:@"我" imageName:@"" selectedImage:@""];
}
/**
* 初始化一個(gè)子控制器
*
* @param childVcClass 子控制器的具體類型
* @param title 子控制器的標(biāo)題
* @param image 子控制器的圖標(biāo)
* @param selectedImage 子控制器的選中圖標(biāo)
*/
- (void)setupChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImage
{
// childVc.view.backgroundColor = CqRandomColor;
childVc.tabBarItem.title = title;
if (imageName.length) {
childVc.tabBarItem.image = [UIImage imageNamed:imageName];
}
if (selectedImage) {
childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
}
[self addChildViewController:childVc];
}
如果系統(tǒng)的tabBar不能滿足需求,可以自己創(chuàng)建自己的tabBar系統(tǒng)的tabBar,根據(jù)用戶添加了多少自控制器來(lái)決定,點(diǎn)擊切換,如果單純?cè)趖abBar添加一個(gè)按鈕,系統(tǒng)的tabBar就不足以讓我們?nèi)ナ褂昧?這就需要我們自己去自定義了.
為tabBar添加按鈕有兩種方法
多添加一個(gè)自控制器,給后來(lái)添加的按鈕留下位置,在添加一個(gè)按鈕覆蓋到添加空的子控制器留的tabBar位置上,這種方法可以實(shí)現(xiàn)我們的需求,但是,會(huì)浪費(fèi)了一個(gè)控制器,直接創(chuàng)建出來(lái)什么,沒(méi)有多大的用處.
另外一種是重新布局tabBar內(nèi)部的排列,一般tabBar是根據(jù)子控制的個(gè)數(shù),將tabBar寬度平均分成自控制器的個(gè)數(shù),我們可以通過(guò)修改每個(gè)自控制器在tabBar現(xiàn)實(shí)的位置進(jìn)行重新分布,為了避免重復(fù)添加按鈕,可以對(duì)按鈕執(zhí)行懶加載.
例如自定義tabBar
- (UIButton *)publishBtn{
if (!_publishBtn) {
UIButton *btn = [[UIButton alloc] init];
[btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[self addSubview:btn];
[btn addTarget:self action:@selector(publishBtnClick) forControlEvents:UIControlEventTouchUpInside];
_publishBtn = btn;
}
return _publishBtn;
}
- (void)layoutSubviews{
[super layoutSubviews];
/** 按鈕的尺寸 **/
CGFloat tabButtonW = self.Cq_width * 0.2;
CGFloat tabButtonH = self.Cq_height;
/** 設(shè)置publishButton的frame **/
self.publishBtn.Cq_width= tabButtonW;
self.publishBtn.Cq_height = tabButtonH;
self.publishBtn.center = CGPointMake(self.Cq_width * 0.5, self.Cq_height * 0.5);
//用來(lái)記錄的索引
int tabBarIndex = 0;
for (UIControl *tabBarButton in self.subviews) {
//如果不是系統(tǒng)UItabBarButton的,跳過(guò)
if (![@"UITabBarButton" isEqualToString:NSStringFromClass(tabBarButton.class)] ){
continue;
}
//布局tabBar子控件的位置
tabBarButton.Cq_width = tabButtonW;
tabBarButton.Cq_height = tabButtonH;
tabBarButton.Cq_y = 0;
tabBarButton.Cq_x = tabBarIndex * tabButtonW;
if (tabBarIndex >= 2) {
tabBarButton.Cq_x += tabButtonW;
}
//記錄點(diǎn)擊的tabBarButton
tabBarButton.tag = tabBarIndex;
//增加索引
tabBarIndex++;
//為了之后監(jiān)聽tabBar點(diǎn)擊每個(gè)頁(yè)面的切換,
[tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
#pragma mark - 監(jiān)聽點(diǎn)擊
- (void)tabBarButtonClick:(UIControl *)tabBarButton{
if (self.selectBtnIndex == tabBarButton.tag) {
//發(fā)通知
[[NSNotificationCenter defaultCenter] postNotificationName:CqTabBarButtonDidRepeatClickNotification object:nil];
}
//記錄索引
self.selectBtnIndex = tabBarButton.tag;
}