Please help me I want to make a colored UITabBar for Each Items I need differentColor.开发者_如何学编程
As explained in this post on Stack Overflow, subclass UITabBarController, then override the -(void)viewDidLoad method. Here's an example:
- (void)viewDidLoad {
[super viewDidLoad];
// Alternate between red and blue. Obviously this is a pretty terrible example,
// since the number of items shouldn't be a fixed integer. Replace this
// with whatever you want to do with your UITabBarItems.
int colorIndex = 0;
NSArray *colorArray = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blueColor], nil];
for (int itemIndex = 0; itemIndex < 3; itemIndex++) {
// Create a frame for a subview which will overlap over the UITabBarItem.
CGRect frame = CGRectMake((self.tabBar.bounds.size.width/2.0)*itemIndex,
0.0,
self.tabBar.bounds.size.width/2.0,
self.tabBar.bounds.size.height);
// Initialize a UIView using the above frame.
UIView *v = [[UIView alloc] initWithFrame:frame];
// Cycle through the colors.
if (++colorIndex >= [colorArray count]) {
colorIndex = 0; // Keep colorIndex from going out of bounds.
}
// Set alpha to 0.5 so OP can see the view overlaps over the original UITabBarItem.
[v setAlpha:0.5];
[v setBackgroundColor:[colorArray objectAtIndex:colorIndex]];
// Add subview to UITabBarController view.
[[self tabBar] addSubview:v];
// v is retained by the parent view, so release it.
[v release];
}
}
Also, you should know that it's good practice to search the answers here on Stack Overflow before asking a new question. You could have made a lot of progress on your own using the link above. You should also post code detailing what you've tried so far--people will be more inclined to help you if you do.
I posted my code because I am still a beginner myself and I enjoyed the challenge presented by your question. If anyone has any suggestions for my code, please comment!
Anyway, hope this helps!
精彩评论