Remove tab bar item text, show only image

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

    Based on all the great answers on this page, I've crafted another solution that also allows you to show the the title again. Instead of removing the content of title, I just change the font color to transparent.

    extension UITabBarItem {
    
        func setTitleColorFor(normalState: UIColor, selectedState: UIColor) {
            self.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: normalState], for: .normal)
            self.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: selectedState], for: .selected)
        }
    
    }
    
    
    extension UITabBarController {
    
        func hideItemsTitle() {
    
            guard let items = self.tabBar.items else {
                return
            }
    
            for item in items {
                item.setTitleColorFor(normalState: UIColor(white: 0, alpha: 0), selectedState: UIColor(white: 0, alpha: 0))
                item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
            }
    
        }
    
        func showItemsTitle() {
    
            guard let items = self.tabBar.items else {
                return
            }
    
            for item in items {
                item.setTitleColorFor(normalState: .black, selectedState: .yellow)
                item.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            }
    
        }
    
    }
    

提交回复
热议问题