簡體   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