简体   繁体   中英

iOS: tab item image not loading

I want to change the image of my tab bar item using self.tabBarItem.image = [UIImage imageNamed:@"home_icon.png"]; Seems simple enough, but I can't get it to run. Note: I'm simply replacing the file name from first to home_icon. The code works with the name "first".

Details: I have started with the standard tabbed application and have only changed this one line of code. Xcode then loads the 64x64 image. I placed a home_icon.png file in the project and made sure it's a 30x30 png file. The docs says: "This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object." Why can't I simple reference a different file here? I don't understand what is actually getting referenced here. The original line is self.tabBarItem.image = [UIImage imageNamed:@"first"]; , to reference first.png.

What I tried (see answers so far) :

  • using home_icon, instead of home_icon.png
  • Using UIImage *img = [UIImage imageNamed:@"home.png"]; self.tabBarItem.image = img; UIImage *img = [UIImage imageNamed:@"home.png"]; self.tabBarItem.image = img;
  • Adding the 2x file
  • Checking output console. No errors mentioned. (The app still references the old file successfully).
  • Checking target. File is added to target

从末尾删除“ .png”。

you should try below code;

UIImage *img = [UIImage imageNamed:@"image.png"];
[[tabBarController.viewControllers objectAtIndex:tabImageIndex] tabBarItem].image=img;

Use the Following Code For Changing The Background Of Tabbar. You can Add different different images According to your Tab.

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControllers{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewControllers];

    switch (index) {
        case 0:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act21.png"]];

            break;
        case 1:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act22.png"]];
            break;
        case 2:
            [tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_act23.png"]];
            break;
        default:
            break;
    }

    return YES;
}

if you want to Change only Tabbar item Image Then Use following Code.

self.tabbarcontroller.tabBarItem.image=[UIImage imageNamed:@"Someimage.png";

Are you sure that [UIImage imageNamed:@"home.png"] return not nil? Check in console

NSLog(@"%@", [UIImage imageNamed:@"home.png"]);

If so, try to add the image to the Build Phases -> Copy Bundle Resources

The solution was to use only alpha channels in the png file. See http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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