UITabBar not showing selected item images in ios 7

后端 未结 20 1624
我在风中等你
我在风中等你 2020-11-29 18:36

The icons show fine in ios 6 but not in ios 7. I\'m setting the selected state in the viewController viewDidLoad method. When the user selects a tab bar item the image disap

相关标签:
20条回答
  • 2020-11-29 18:58

    I had a similar problem. I created the tab bar in storyboard and added all the images through the interface builder menus, none in code.

    My fix was actually simple: under the attributes inspector window in IB, the Tab Bar Item field for "Selected Image" should be blank, and the Bar Item field for "Image" should be filled with the image you want.

    I am running Xcode 6.0.1 and testing with iOS 8.0.2 devices.

    0 讨论(0)
  • 2020-11-29 19:01

    I had the same problem with Xcode 6.0.1 (6A317), seems to be a bug with Interface builder. However, i've managed to solve it by leaving selected image empty in interface builder then at each viewDidLoad in my view controllers i did insert:

    [self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]];

    it works well now, showing my selectedImage and with global tint mask.

    0 讨论(0)
  • 2020-11-29 19:02

    I had the same issue. But working with StoryBoards prevented me from changing anything in the code. Leaving the image empty in the storyboard removed this obstacle for me. However putting the initWithTitle in the viewWillAppear method of the tab's viewcontroller gave me odd behaviour. First getting the selected image required an additional click and images did not show up for the non-initial tabs.

    For me fixing this was adding the following code to the AppDelegate in the DidFinishLoadingWithOptions (similar to 132206 and Amitabha):

    NSArray * vcs = [(UITabBarController*)self.window.rootViewController viewControllers];
    UIViewController *tab0 = [[(UINavigationController*)[vcs objectAtIndex:0] viewControllers] objectAtIndex:0];
    tab0.title = NSLocalizedString(@"Time", nil);
    tab0.tabBarItem =  [[UITabBarItem alloc] initWithTitle:tab0.title image:[UIImage imageNamed:@"Recents.png"] selectedImage:[UIImage imageNamed:@"RecentsSolid.png"]];
    UIViewController *tab1 = [[(UINavigationController*)[vcs objectAtIndex:1] viewControllers] objectAtIndex:0];
    tab1.title = NSLocalizedString(@"Expense", nil);
    tab1.tabBarItem = [[UITabBarItem alloc] initWithTitle:tab1.title image:[UIImage imageNamed:@"Euro.png"] selectedImage:[UIImage imageNamed:@"EuroSolid.png"]];
    
    0 讨论(0)
  • 2020-11-29 19:02

    After trying all of the other answers and struggling as they failed, I found the answer. The other answers, , don't seem to work in current swift version. In Swift 2.3, this works for me. For those who are still having trouble, try this:

    tabBarItem.image = UIImage(named: "image_name")
    searchVC.tabBarItem.selectedImage = UIImage(named:
         "image_name_when_selected")?.imageWithRenderingMode(.AlwaysOriginal)
    
    0 讨论(0)
  • 2020-11-29 19:03

    Use the code below to fix the image issue in iOS7:

    [[UITabBarItem alloc] initWithTitle:@"title" image:[[UIImage imageNamed:@"YOUR_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"YOUR_SEL_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    0 讨论(0)
  • 2020-11-29 19:04

    Old questions, but going to add my solution here as well for Xamarin.iOS/C# and those who want to set the images via Interface builder. I set the Selected Image and Image attributes via Interface Builder. Then in code I defined an InitTabs() method like this:

    public void InitTabs(){
            HomeTab.SelectedImage = HomeTab.SelectedImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
            HomeTab.Image = HomeTab.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
    }
    

    Call InitTabs() in ViewDidLoad and now the proper image will appear for both the selected and unselected state.

    0 讨论(0)
提交回复
热议问题