方法一:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
CGRect frame = CGRectMake(0, 0, 320, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *img = [UIImage imageNamed:@"tabbar.png"];
UIColor *color = [[UIColor alloc] initWithPatternImage:img];
v.backgroundColor = color;
[tabBarController.tabBar insertSubview:v atIndex:0];
tabBarController.tabBar.opaque = YES;
[color release];
[v release];
方法二:
默认UITabBarController的tabBar背景是黑色的。可以按 下面方法:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *array = [tabBarController.view subviews];
UITabBar *tabBar = [array objectAtIndex:1];
UIImage *image = [UIImage imageWithContentsOfFile:sourcePath ];
tabBar.layer.contents = (id)image.CGImage;
第二种可以去掉上面一像素的横线。
更改tabbarItem的图片
NSArray *itemArray = [[[rootTabBarController.viewsubviews] objectAtIndex:1]subviews];
for (int i = 0; i < itemArray.count; i++) {
UIImageView *image = [[UIImageView alloc] init];
UIView *v = [itemArray objectAtIndex:i];
if (i == 0) {
image.image = [UIImageimageNamed:@"Tab_bar_11.png"];
}else {
image.image = [UIImageimageNamed:[NSStringstringWithFormat:@"Tab_bar_0%d.png",i +1]];
}
image.frame =CGRectMake(23,3, 30, 30);
[v addSubview:image];
[image release],image = nil;
}
然后在delegate 里面
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == [tabBarController.viewControllersindexOfObject:viewController]) {
return NO;
}
NSArray *itemArray = [[[tabBarController.viewsubviews] objectAtIndex:1]subviews];
for (int i = 0; i < itemArray.count; i++) {
UIImageView *image = [[UIImageView alloc] init];
UIView *v = [itemArray objectAtIndex:i];
if (i == [tabBarController.viewControllersindexOfObject:viewController]) {
image.image = [UIImageimageNamed:[NSStringstringWithFormat:@"Tab_bar_1%d.png",i +1]];
}else {
image.image = [UIImageimageNamed:[NSStringstringWithFormat:@"Tab_bar_0%d.png",i +1]];
}
image.frame =CGRectMake(23,3, 30, 30);
NSArray *imageArr = [v subviews];
for (UIView *v in imageArr) {
if ([v isKindOfClass:[UIImageViewclass]]) {
[v removeFromSuperview];
}
if ([subview isKindOfClass:[UILabel class]])
// {
// UILabel *label = (UILabel *)subview;
//
// UILabel *newLabel = [[UILabel alloc] init];
// newLabel.font = label.font;
// newLabel.text = label.text;
// newLabel.textColor = [UIColor redColor];
// newLabel.backgroundColor = [UIColor clearColor];
// newLabel.opaque = YES;
// newLabel.frame = CGRectMake(0, 0, label.frame.size.width, label.frame.size.height + 1);
// [subview addSubview:newLabel];
// [newLabel release];
// }
}
[v addSubview:image];
[image release],image = nil;
}
}