UITabBar not showing selected item images in ios 7

后端 未结 20 1622
我在风中等你
我在风中等你 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 19:10

    Swift version of showing selected and unselected images and title with UIAppearance API In your Appdelegate.m copy following code if you have tab base app.following code assume you have 4 tab bar.

    let tabBarController: UITabBarController = (self.window!.rootViewController as! UITabBarController)
    
        let tabBar:UITabBar = tabBarController.tabBar
    
        let tabBarItem1:UITabBarItem = tabBar.items![0]
        let tabBarItem2:UITabBarItem = tabBar.items![1]
        let tabBarItem3:UITabBarItem = tabBar.items![2]
        let tabBarItem4:UITabBarItem = tabBar.items![3]
    
        tabBarItem1.title = "Home";
        tabBarItem2.title = "Maps";
        tabBarItem3.title = "My Plan";
        tabBarItem4.title = "Settings";
    
        tabBarItem1.selectedImage = UIImage(named: "home_selected.png")!
        tabBarItem2.selectedImage = UIImage(named: "maps_selected.png")!
        tabBarItem3.selectedImage = UIImage(named: "myplan_selected.png")!
        tabBarItem4.selectedImage = UIImage(named: "settings_selected.png")!
    
         tabBarItem1.image =  UIImage(named: "home.png")!
         tabBarItem2.image =  UIImage(named: "maps.png")!
         tabBarItem3.image =  UIImage(named: "myplan.png")!
         tabBarItem4.image =  UIImage(named: "settings.png")!
    
    
        let tabBarBackground: UIImage = UIImage(named: "tabbar.png")!
        UITabBar.appearance().backgroundImage = tabBarBackground
        UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabbar_selected.png")!
    
    
        UITabBarItem.appearance().setTitleTextAttributes([
            NSForegroundColorAttributeName : UIColor.whiteColor()
            ]
            , forState: .Normal)
        let titleHighlightedColor: UIColor = UIColor(red: 153 / 255.0, green: 192 / 255.0, blue: 48 / 255.0, alpha: 1.0)
        UITabBarItem.appearance().setTitleTextAttributes([
            NSForegroundColorAttributeName : titleHighlightedColor
            ]
            , forState: .Highlighted)
    
    0 讨论(0)
  • 2020-11-29 19:12

    You need to use tabBarItem initWithTitle:image:selectedImage

    [[UITabBarItem alloc] initWithTitle:@"title" image:image selectedImage:imageSel];
    

    in conjunction with changing the UIImage rendering mode:

    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
    

    or (to apply parent views template tint mask, this option is default for Tab bar Items unless you opt out with the above rendering mode)

    imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
    

    here is a code sample for one tab bar item :-

    UIImage *musicImage = [UIImage imageNamed:@"music.png"];
    UIImage *musicImageSel = [UIImage imageNamed:@"musicSel.png"];
    
    musicImage = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    musicImageSel = [musicImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.musicViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Music" image:musicImage selectedImage:musicImageSel];
    

    Hope this helps

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