UITabBar
1.自定义tabbar过程及注意:
1.新建一个tabBarViewController继承于UITabBarViewController.
2. 在appdelegate中将tabBarViewController的对象作为根。
MyTabBarViewController *tabBar =[[MyTabBarViewController alloc]init];
self.window.rootViewController = tabBar ;
[tabBar release];
3. tabBarViewController中创建tabbar和实例化VC
主要就是一个UIImageView再加上循环码button,贴在UIImageView上。
码btn过程:
for(int i =0;i<3;i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(40+i*(40+50),0 ,40 , 40);
[btn setImage:[UIImage imageNamed:grayArr[i]]forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:lightArr[i]]forState:UIControlStateSelected];
[btn addTarget:self action:@selector(btnDown:)forControlEvents:UIControlEventTouchUpInside];
btn.tag = 1000+i;
[imgView addSubview:btn];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(49+i*90, 37,40, 10)];
label.text = labArr[i];
label.font = [UIFont systemFontOfSize:11];
[imgView addSubview:label];
label.tag = 2000+i;
if(i ==0)
{
btn.selected = YES;
label.textColor = [UIColororangeColor];
}
[label release];
}
然后再btnDown中实现对button上图片颜色和字体颜色点击后变化的控制。
-(void)btnDown:(UIButton *)btn
{
for(int i =0;i<3;i++)
{
UIButton *tempBtn = (UIButton *)[self.view viewWithTag:1000+i];
UILabel *tempLab = (UILabel *)[self.view viewWithTag:2000+i];
if(tempBtn == btn)
{
tempBtn.selected = YES;
tempLab.textColor = [UIColor orangeColor];
}
else
{
tempBtn.selected = NO;
tempLab.textColor = [UIColor colorWithRed:149/255.0 green:149/255.0blue:149/255.0 alpha:1];
}
}
self.selectedIndex= btn.tag-1000;//显示对应的VC。
}
特别注意,循环完后一定要写self.selectedIndex = btn.tag-1000;//显示对应的VC。。不然VC视图无法切换。
创建视图过程:
1、
ShouYeViewController *shouye = [[ShouYeViewControlleralloc]init];
PinDaoViewController *pindao = [[PinDaoViewController alloc]init];
GeRenViewController *geren = [[GeRenViewController alloc]init];
2、
UINavigationController *oneNav = [[UINavigationControlleralloc]initWithRootViewController:shouye];
UINavigationController *twoNav = [[UINavigationControlleralloc]initWithRootViewController:pindao];
UINavigationController *threeNav = [[UINavigationControlleralloc]initWithRootViewController:geren];
[shouye release];
[pindao release];
[geren release];
3、
NSArray *arr = @[oneNav,twoNav,threeNav];
[oneNav release];
[twoNav release];
[threeNav release];
4、
self.viewControllers = arr;
源码参看:点击打开链接
练习项目参看:点击打开链接