簡體   English   中英

更改uitabbar項目圖像的按鈕

[英]Button that change uitabbar item's image

我創建了帶有3個選項卡項的UITabBarVontroller 我還為每個項目設置了圖像和標題。 在第一個視圖中(對應於選項卡欄中的左側項目),我放置了一個啟動方法的按鈕:
-launchBtn()

我的目標是當我按下按鈕時要更改2號項目的圖像。

新編輯:
我每次啟動該方法時都要使新圖像閃爍一次(僅一次)。

您可以使用以下代碼:

- (IBAction)launchBtn:(id)sender {
    // Retrieve UITabBarController
    UITabBarController *tabBarController = (UITabBarController *)[[[UIApplication sharedApplication] delegate] window].rootViewController;

    // Get ViewControllers array
    NSArray *viewControllers = tabBarController.viewControllers;

    // Cast the ViewController you are interested and push the image when unselected 
    ((SecondViewController*)[viewControllers objectAtIndex:1]).tabBarItem.image = [[UIImage imageNamed:@"camera"] imageWithRenderingMode:UIImageRenderingModeAutomatic];

    // Cast the ViewController you are interested and push the image when selected 
    ((SecondViewController*)[viewControllers objectAtIndex:1]).tabBarItem.selectedImage = [[UIImage imageNamed:@"camera"] imageWithRenderingMode:UIImageRenderingModeAutomatic];

    // You can use this method to push the ViewController, too.
    //[tabBarController setSelectedIndex:1];

    // Blink image
    [self blink:((SecondViewController*)[viewControllers objectAtIndex:1])];
}


// Blink image
- (void)blink: (SecondViewController*) viewControllers {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        // Cast the ViewController you are interested and push the image when unselected
        viewControllers.tabBarItem.image = nil;

        // Cast the ViewController you are interested and push the image when selected
        viewControllers.tabBarItem.selectedImage = nil;

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            // Cast the ViewController you are interested and push the image when unselected
            viewControllers.tabBarItem.image = [[UIImage imageNamed:@"camera"] imageWithRenderingMode:UIImageRenderingModeAutomatic];

            // Cast the ViewController you are interested and push the image when selected
            viewControllers.tabBarItem.selectedImage = [[UIImage imageNamed:@"camera"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
        });
    });
}

您可以從此處下載示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM