如何在选择标签栏时更改图像。帮帮我吧谢谢。
答案 0 :(得分:1)
您可以在此处找到有关如何创建自定义标签栏
的信息您可以按照以下代码设置图像&图像选择为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
我希望,这可以帮助你很多:)
答案 1 :(得分:1)
您可以使用UITabBarControllerDelegate方法
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if(tabBarController.selectedIndex==0)
{
[viewController.tabBarItem setImage:[UIImage imageNamed:@"home.png"]];
}
}
在appDelegate.m文件中使用此代码 并在appDelegate.h文件中添加协议
答案 2 :(得分:0)
您可以制作自定义标签栏: 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
中您在传递选择该标签栏项目时显示的图像的位置
答案 3 :(得分:0)
我认为你需要尝试这个,希望这会有所帮助,
我更改了选定的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"]];