Remove tab bar item text, show only image

前端 未结 19 2162
我寻月下人不归
我寻月下人不归 2020-12-04 06:50

Simple question, how can I remove the tab bar item text and show only the image?

I want the bar items to like in the instagram app:

19条回答
  •  生来不讨喜
    2020-12-04 07:22

    I used the following code in my BaseTabBarController's viewDidLoad. Note that in my example, I have 5 tabs, and selected image will always be base_image + "_selected".

    // Get tab bar and set base styles
    let tabBar = self.tabBar;
    tabBar.backgroundColor = UIColor.whiteColor()
    
    // Without this, images can extend off top of tab bar
    tabBar.clipsToBounds = true
    
    // For each tab item..
    let tabBarItems = tabBar.items?.count ?? 0
    for i in 0 ..< tabBarItems {
        let tabBarItem = tabBar.items?[i] as UITabBarItem
    
        // Adjust tab images (Like mstysf says, these values will vary)
        tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -6, 0);
    
        // Let's find and set the icon's default and selected states
        // (use your own image names here)
        var imageName = ""
        switch (i) {
        case 0: imageName = "tab_item_feature_1"
        case 1: imageName = "tab_item_feature_2"
        case 2: imageName = "tab_item_feature_3"
        case 3: imageName = "tab_item_feature_4"
        case 4: imageName = "tab_item_feature_5"
        default: break
        }
        tabBarItem.image = UIImage(named:imageName)!.imageWithRenderingMode(.AlwaysOriginal)
        tabBarItem.selectedImage = UIImage(named:imageName + "_selected")!.imageWithRenderingMode(.AlwaysOriginal)
    }
    

提交回复
热议问题