繁体   English   中英

如何自定义 UITabBar - 在选择标签栏时如何更改所选标签栏的图像

[英]How to Customize UITabBar - On selection of tabbar how to change the image of selected tabbar

如何在选择标签栏时更改图像。 帮我解决这个问题 谢谢。

您可以在此处找到有关如何创建自定义标签栏的信息

http://www.rumexit.co.uk/2010/07/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-1-of-2/

您可以按照下面的代码为 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. 创建标签栏视图 controller 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];

}

在您的标签栏中 controller viewDidLoad从这种方式调用此方法:

- (void)viewDidLoad
{
    [self addCenterButtonWithImage:[UIImage imageNamed:@"bemobile.png"] highlightImage:[UIImage imageNamed:@"bemobileSelected.png"]];

    [super viewDidLoad];
}

highlightImage中,您传递 select 该选项卡栏项时将显示的图像

我认为你需要尝试这个,希望这会有所帮助,

我已经更改了选定的 tabbatitem 图像,例如 -

在标签栏中 controller 委托方法

- (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"]];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM