Remove tab bar item text, show only image

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

    iOS 11 throws a kink in many of these solutions, so I just fixed my issues on iOS 11 by subclassing UITabBar and overriding layoutSubviews.

    class MainTabBar: UITabBar {
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            // iOS 11: puts the titles to the right of image for horizontal size class regular. Only want offset when compact.
            // iOS 9 & 10: always puts titles under the image. Always want offset.
            var verticalOffset: CGFloat = 6.0
    
            if #available(iOS 11.0, *), traitCollection.horizontalSizeClass == .regular {
                verticalOffset = 0.0
            }
    
            let imageInset = UIEdgeInsets(
                top: verticalOffset,
                left: 0.0,
                bottom: -verticalOffset,
                right: 0.0
            )
    
            for tabBarItem in items ?? [] {
                tabBarItem.title = ""
                tabBarItem.imageInsets = imageInset
            }
        }
    }
    

提交回复
热议问题