如何自定义UITabBar - 选择tabbar如何更改所选tabbar的图像
答
您可以在这里找到有关如何创建自定义标签栏
而且你可以按照码下面的设置图片&被选择用于UIControlStateNormal & UIControlStateSelected
UIImage *btnImage = [UIImage imageNamed:@"Button_Normal.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"Bouton_Selected.png"];
self.bouton_tab = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
bouton_tab.frame = CGRectMake(xStart, TABYSTART, TABITEMWIDTH, TABITEMHEIGHT); // Set the frame (size and position) of the button)
[bouton_tab setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
[bouton_tab setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button
[bouton_tab setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[bouton_tab setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially
希望,这可以帮助你很多:)
答
你可以使用UITabBarControllerDelegate方法
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if(tabBarController.selectedIndex==0)
{
[viewController.tabBarItem setImage:[UIImage imageNamed:@"home.png"]];
}
}
使用此代码在appDelegate.m文件 并添加appDelegate.h一个协议文件
答
您可以自定义标签栏: 1.创建标签栏视图控制器 2。在这个VC把这个方法:
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
self.button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[self.button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
self.button.center = self.tabBar.center;
[self.view addSubview: self.button];
}
在你的标签栏控制器viewDidLoad
调用此方法从这样:
- (void)viewDidLoad
{
[self addCenterButtonWithImage:[UIImage imageNamed:@"bemobile.png"] highlightImage:[UIImage imageNamed:@"bemobileSelected.png"]];
[super viewDidLoad];
}
凡highlightImage
传递将要显示的图像时,选择该选项卡栏项目
答
我想你需要尝试这一块,希望这会有所帮助,
我有改变选择tabbatitem图像等 -
中的TabBar控制器委托方法- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([tabBarController selectedIndex] == 0)
{
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
}
}
通过这个
你可以改变你的形象。
或者你可以在你的视图控制器的init(或viewWillAppear中)的方法直接使用,如
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
更好您寻找多一点,这个问题已经在上个月问了很多次。 – rptwsthi 2011-06-13 09:09:51