Remove tab bar item text, show only image

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

    Swift version of ddiego answer

    Compatible with iOS 11

    Call this function in viewDidLoad of every first child of the viewControllers after setting title of the viewController

    Best Practice:

    Alternativelly as @daspianist suggested in comments

    Make a subclass of like this class BaseTabBarController: UITabBarController, UITabBarControllerDelegate and put this function in the subclass's viewDidLoad

    func removeTabbarItemsText() {
    
        var offset: CGFloat = 6.0
    
        if #available(iOS 11.0, *), traitCollection.horizontalSizeClass == .regular {
            offset = 0.0
        }
    
        if let items = tabBar.items {
            for item in items {
                item.title = ""
                item.imageInsets = UIEdgeInsets(top: offset, left: 0, bottom: -offset, right: 0)
            }
        }
    }
    

提交回复
热议问题