您可以为UITabBarController使用自定义类并重写tabBarController。在此处,您可以设置所需按钮及其图像与操作。
以下是自定义选项卡栏控制器类的示例:
// CustomTabBarController.h
#import <UIKit/UIKit.h>
@interface CustomTabBarController : UITabBarController {
UIButton *settingsButton;
UIButton *infoButton;
UIButton *aboutUsButton;
}
@property (nonatomic, retain) UIButton *settingsButton;
@property (nonatomic, retain) UIButton *infoButton;
@property (nonatomic, retain) UIButton *aboutUsButton;
-(void) addCustomElements;
-(void) selectTab:(int)tabID;
@end
// CustomTabBarController.m
#import "CustomTabBarController.h"
@implementation CustomTabBarController
@synthesize settingsButton, infoButton, aboutUsButton;
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self addCustomElements];
}
-(void)addCustomElements
{
UIImageView* bgView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBarBackground.png"]] autorelease];
bgView.frame = CGRectMake(0, 420, 320, 60);
[self.view addSubview:bgView];
UIImage *btnImage = [UIImage imageNamed:@"settings.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"settingsSelected.png"];
self.settingsButton = [UIButton buttonWithType:UIButtonTypeCustom];
settingsButton.frame = CGRectMake(10, 426, 100, 54);
[settingsButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];
[settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[settingsButton setBackgroundImage:btnImageSelected forState:UIControlStateDisabled];
[settingsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];
[settingsButton setTag:101];
[settingsButton setSelected:true];
btnImage = [UIImage imageNamed:@"info.png"];
btnImageSelected = [UIImage imageNamed:@"infoSelected.png"];
self.infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
infoButton.frame = CGRectMake(110, 426, 100, 54);
[infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[infoButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[infoButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];
[infoButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];
[infoButton setTag:102];
btnImage = [UIImage imageNamed:@"aboutUs.png"];
btnImageSelected = [UIImage imageNamed:@"aboutUsSelected.png"];
self.aboutUsButton = [UIButton buttonWithType:UIButtonTypeCustom];
aboutUsButton.frame = CGRectMake(210, 426, 100, 54);
[aboutUsButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[aboutUsButton setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[aboutUsButton setBackgroundImage:btnImageSelected forState:UIControlStateHighlighted];
[aboutUsButton setImage:btnImageSelected forState:(UIControlStateHighlighted|UIControlStateSelected)];
[aboutUsButton setTag:103];
[self.view addSubview:settingsButton];
[self.view addSubview:infoButton];
[self.view addSubview:aboutUsButton];
[settingsButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[infoButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aboutUsButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonClicked:(id)sender
{
int tagNum = [sender tag];
[self selectTab:tagNum];
}
- (void)selectTab:(int)tabID
{
switch(tabID)
{
case 101:
[settingsButton setSelected:true];
[infoButton setSelected:false];
[aboutUsButton setSelected:false];
break;
case 102:
[settingsButton setSelected:false];
[infoButton setSelected:true];
[aboutUsButton setSelected:false];
break;
case 103:
[settingsButton setSelected:false];
[infoButton setSelected:false];
[aboutUsButton setSelected:true];
break;
}
self.selectedIndex = tabID;
}
- (void)dealloc {
[settingsButton release];
[infoButton release];
[aboutUsButton release];
[super dealloc];
}
@end
希望这能对你有很大的帮助。