Remove tab bar item text, show only image

前端 未结 19 2088
我寻月下人不归
我寻月下人不归 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:26

    If you're using storyboards this would be you best option. It loops through all of the tab bar items and for each one it sets the title to nothing and makes the image full screen. (You must have added an image in the storyboard)

    for tabBarItem in tabBar.items!
    {
       tabBarItem.title = ""
       tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
    }
    
    0 讨论(0)
  • 2020-12-04 07:27

    If you are looking to center the tabs / change the image insets without using magic numbers, the following has worked for me (in Swift 5.2.2):

    In a UITabBarController subclass, you can add add the image insets after setting the view controllers.

    override var viewControllers: [UIViewController]? {
        didSet {
          addImageInsets()
        }
    }
    
    func addImageInsets() {
        let tabBarHeight = tabBar.frame.height
    
        for item in tabBar.items ?? [] where item.image != nil {
          let imageHeight = item.image?.size.height ?? 0
          let inset = CGFloat(Int((tabBarHeight - imageHeight) / 4))
          item.imageInsets = UIEdgeInsets(top: inset,
                                          left: 0,
                                          bottom: -inset,
                                          right: 0)
        }
    }
    

    Several of the options above list solutions for dealing with hiding the text.

    0 讨论(0)
  • 2020-12-04 07:28

    You should play with imageInsets property of UITabBarItem. Here is sample code:

    let tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "more")
    tabBarItem.imageInsets = UIEdgeInsets(top: 9, left: 0, bottom: -9, right: 0)
    

    Values inside UIEdgeInsets depend on your image size. Here is the result of that code in my app:

    enter image description here

    0 讨论(0)
  • 2020-12-04 07:28

    make a subclass of UITabBarController and assign that to your tabBar , then in the viewDidLoad method place this line of code:

    tabBar.items?.forEach({ (item) in
            item.imageInsets = UIEdgeInsets.init(top: 8, left: 0, bottom: -8, right: 0)
        })
    
    0 讨论(0)
  • 2020-12-04 07:32

    Using approach with setting each UITabBarItems title property to "" and update imageInsets won't work properly if in view controller self.title is set. For example if self.viewControllers of UITabBarController are embedded in UINavigationController and you need title to be displayed on navigation bar. In this case set UINavigationItems title directly using self.navigationItem.title, not self.title.

    0 讨论(0)
  • 2020-12-04 07:32

    Swift 4 approach

    I was able to do the trick by implementing a function that takes a TabBarItem and does some formatting to it.

    Moves the image a little down to make it be more centered and also hides the text of the Tab Bar. Worked better than just setting its title to an empty string, because when you have a NavigationBar as well, the TabBar regains the title of the viewController when selected

    func formatTabBarItem(tabBarItem: UITabBarItem){
        tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.clear], for: .selected)
        tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.clear], for: .normal)
    }
    

    Latest syntax

    extension UITabBarItem {
        func setImageOnly(){
            imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.clear], for: .selected)
            setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.clear], for: .normal)
        }
     }
    

    And just use it in your tabBar as:

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