creatNewControllerWithSelectImageName:SelectImageName];
}
- (void)creatNewControllerWithSelectImageName:(NSString *)selectImageName
{
//创建的control的宽度
CGFloat controlWidth = [UIScreen mainScreen].bounds.size.width/self.viewControllers.count;
//初始化选中的control的背景视图
_selectImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,controlWidth, 49)];
_selectImage.image = [UIImage imageNamed:selectImageName];
[_newTabBar addSubview:_selectImage];
//首先通过for循环了解产生controller的数量
for (int i = 0; i < self.viewControllers.count; i ++) {
//自定义控件
UIControl *newControl = [[UIControl alloc] initWithFrame:CGRectMake(i * controlWidth, 0, controlWidth, 45)];
//将自定义的tabBarButton 添加到 自定义的tabBar上
[_newTabBar addSubview:newControl];
//添加点击效果
[newControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
//通过设置tag方便传值
newControl.tag = 1000 + i;
//得到当前的控制器
UIViewController *viewC = [self.viewControllers objectAtIndex:i];
//拿到之前的图片和信息
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = viewC.tabBarItem.image;
UILabel *label = [[UILabel alloc] init];
label.text = viewC.tabBarItem.title;
}
}
//
- (void)controlAction:(UIControl *)control
{
self.selectedIndex = control.tag - 1000; //通过获得的tag值进行切换ViewController
//移动动画效果的实现
[UIView animateWithDuration:.4 animations:^{
_selectImage.center = control.center;
}];
}
/*
细节很重要
(1)用tabBar的selectedIndex属性,可以让自己创建的按钮关联到ViewController
(2)取消系统的高亮:可以定义一个按钮,覆写里面的setHighlied方法,什么也不做就行了
(3)关于几个按钮只选中一个的方法:设置一个属性,记录上一个选中的按钮,点击当前按钮,把上一个按钮设置为未选中,并把当前的按钮设置为选中,最后把当前按钮赋值给上一个按钮
*/
|