Second Chapter
创建程序的主架构
- 大多数一般都使用tabBarController嵌套着navgationCotroller,系统默认的tabBarItem如果不是我们需要的,要想修改,可以统一通过UIAppearance来设置.
- 只要方法名或者属性名后面带有UI_APPEARANCE_SELECTOR这个宏,就可以通过[XXX appearance]对象来统一设置
//设置普通状态文字的状态
NSMutableDictionary *normalAtts = [NSMutableDictionary dictionary];
normalAtts[NSForegroundColorAttributeName] = [UIColor grayColor];
normalAtts[NSFontAttributeName] = [UIFont systemFontOfSize:14];
//设置选中状态文字的状态
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:@""];
}
/**
* 初始化一个子控制器
*
* @param childVcClass 子控制器的具体类型
* @param title 子控制器的标题
* @param image 子控制器的图标
* @param selectedImage 子控制器的选中图标
*/
- (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];
}
如果系统的tabBar不能满足需求,可以自己创建自己的tabBar系统的tabBar,根据用户添加了多少自控制器来决定,点击切换,如果单纯在tabBar添加一个按钮,系统的tabBar就不足以让我们去使用了,这就需要我们自己去自定义了.
为tabBar添加按钮有两种方法
多添加一个自控制器,给后来添加的按钮留下位置,在添加一个按钮覆盖到添加空的子控制器留的tabBar位置上,这种方法可以实现我们的需求,但是,会浪费了一个控制器,直接创建出来什么,没有多大的用处.
另外一种是重新布局tabBar内部的排列,一般tabBar是根据子控制的个数,将tabBar宽度平均分成自控制器的个数,我们可以通过修改每个自控制器在tabBar现实的位置进行重新分布,为了避免重复添加按钮,可以对按钮执行懒加载.
例如自定义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;
/** 设置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);
//用来记录的索引
int tabBarIndex = 0;
for (UIControl *tabBarButton in self.subviews) {
//如果不是系统UItabBarButton的,跳过
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;
}
//记录点击的tabBarButton
tabBarButton.tag = tabBarIndex;
//增加索引
tabBarIndex++;
//为了之后监听tabBar点击每个页面的切换,
[tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
#pragma mark - 监听点击
- (void)tabBarButtonClick:(UIControl *)tabBarButton{
if (self.selectBtnIndex == tabBarButton.tag) {
//发通知
[[NSNotificationCenter defaultCenter] postNotificationName:CqTabBarButtonDidRepeatClickNotification object:nil];
}
//记录索引
self.selectBtnIndex = tabBarButton.tag;
}