大家好,这么晚了,因为我昨天再接过的项目是,发现外包的伙伴们写的Tabbar是系统的,因为我们的产品要求的是44的高度,而系统的Tabbar的高度是49像素,所以怎么办呢,外包的伙伴们做的是在系统的Tabbar的Item上做的视图覆盖,这样我发现有白色的5像素视图会在iPhone5一下的设备并且是IOS7一下的系统上出现,那么怎么办呢,当然做好的办法是不是补补改改,最好的就是从跟上解决,这样我们框架上的其他东西就不会导致一些问题的出现,所以就要自定义Tabbar,
系统给了一个好的方法:切换视图控制器的:
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);
//这个要注意的一点事一定要判断是否finished 做一个BOOL判断,如果没有判断,你要快速的点击切换,会出现白屏,因为你的某一个Controller有refresh或者reload之类的,影响切换的速度,所以一定要做个bool值判断,下来这家看代码吧:
- (void)viewDidLoad
{
[superviewDidLoad];
isTransted = YES;
//主控制器的内容视图
_contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 44)];
[self.viewaddSubview:_contentView];
//主控制器的
_toolView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - 44, self.view.width,44)];
_toolView.backgroundColor = [UIColorredColor];
_toolView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:_toolView];
messages = [[Messagesalloc] init];
Grade *grade = [[Grade alloc] init];
PhotoTakeCenterVC *tvc = [[PhotoTakeCenterVCalloc]init];
ListenClassListController *listen = [[ListenClassListControlleralloc]init];
Setting *setting = [[Setting alloc] init];
[selfaddChildViewController:messages];
[selfaddChildViewController:grade];
[selfaddChildViewController:tvc];
[selfaddChildViewController:listen];
[selfaddChildViewController:setting];
UIViewController *firstController = self.childViewControllers[0];
_selectedIndex = 1;
firstController.view.top = 0;
for (NSInteger i = 0; i < count; i ++)
{
CGFloat width = 60;
CGFloat height = 44;
CGFloat y = 524 - AutoHeight;
CGFloat x = (width + 1)*i;
if (i == 2)
{
width = 76;
height = 54;
y -= 10;
}
else if (i > 2)
{
x += 15;
}
CGFloat lineY = y;
if (i == 2)
{
lineY += 10;
}
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(x + width, 0, 2, height)];
lineView.backgroundColor = COLOR_WITH_RGB(28.0f, 30.0f, 36.0f);
[_toolView addSubview:lineView];
[lineView release];
if (iPhone5) {
y = 568 - 44;
}
else
{
y = 480 - 44;
}
if (i != 2) {
BarItem *button = [[BarItem alloc] initWithFrame:CGRectMake(x, 0, width, height)];
NSString *normal = [NSString stringWithFormat:@"TabItem%d_0",i];
[button setImage:LOADIMAGE(normal, @"png") forState:UIControlStateNormal];
NSString *selected = [NSString stringWithFormat:@"NewTabItem%d_1",i];
[button setImage:LOADIMAGE(selected, @"png") forState:UIControlStateSelected];
button.badgeValue = @"0";
button.tag = i + ITEM_TAG - 999;
[_toolView addSubview:button];
if (i == 0)
{
button.selected = YES;
[[NSNotificationCenterdefaultCenter] postNotificationName:@"UPDATE_ITEM_BADGEVALUE"object:chattype_chat];
}
[button release];
[button addTarget:selfaction:@selector(selectControl:) forControlEvents:UIControlEventTouchUpInside];
self.selectedIndex = 1;
}
if(i == 2)
{
BarItem *button = [[BarItem alloc] initWithFrame:CGRectMake(x, -10, width, height)];
NSString *normal = [NSString stringWithFormat:@"TabItem%d_0",i];
[button setImage:LOADIMAGE(normal, @"png") forState:UIControlStateNormal];
NSString *selected = [NSString stringWithFormat:@"NewTabItem%d_1",i];
[button setImage:LOADIMAGE(selected, @"png") forState:UIControlStateSelected];
button.badgeValue = @"0";
button.tag = i + ITEM_TAG -999;
[_toolView addSubview:button];
[button addTarget:selfaction:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
[button release];
}
}
[_contentView addSubview:firstController.view];
}
- (void)selectControl:(BarItem *)sender
{
if (isTransted == NO) {
return;
}
;
if (_selectedIndex != sender.tag)
{
isTransted = NO;
UIViewController *toViewController = self.childViewControllers[sender.tag - 1];
toViewController.view.top = 0;
[selftransitionFromViewController:self.childViewControllers[_selectedIndex - 1] toViewController:toViewController duration:0options:0animations:^{
} completion:^(BOOL finished) {
isTransted = YES;
}];
//变换控制器
_selectedIndex = sender.tag;
}
for (BarItem *btn in_toolView.subviews)
{
if ([btn isKindOfClass:[BarItem class]])
{
btn.selected = NO;
NSString *normal = [NSString stringWithFormat:@"TabItem%d_0",btn.tag - 1];
[btn setImage:LOADIMAGE(normal, @"png") forState:UIControlStateNormal];
}
}
BarItem *btn1 = (BarItem *)[_toolView viewWithTag:sender.tag];
NSString *selected = [NSString stringWithFormat:@"NewTabItem%d_1",sender.tag - 1];
[btn1 setImage:LOADIMAGE(selected, @"png") forState:UIControlStateNormal];
btn1.selected = YES;
}
这里的BarItem是一个自定义的button
大家尝试吧。其实还有比这更好的,那个是封装的,资源code4App里有。