如何将自定义彩色图像放入 UITabBar
How to put custom colorful image in UITabBar
我想向 UITabBar 添加自定义图像,但它失去了颜色,我猜这是因为 tintColor。
What I get
What I want
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
UITabBar.appearance().backgroundColor = .menuColor
UITabBar.appearance().tintColor = .white
UITabBar.appearance().unselectedItemTintColor = .gray
setupVCs()
}
private func createNavController(for rootViewController: UIViewController,
title: String,
image: UIImage,
imageInsets: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) -> UIViewController {
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.title = title
navController.tabBarItem.image = image
navController.tabBarItem.imageInsets = imageInsets
navController.navigationBar.prefersLargeTitles = true
rootViewController.navigationItem.title = title
return navController
}
private func setupVCs() {
viewControllers = [
createNavController(for: ViewController(), title: NSLocalizedString("notes", comment: ""), image: UIImage(named: "notes")!),
createNavController(for: ViewController(), title: NSLocalizedString("", comment: ""), image: UIImage(named: "rec")!, imageInsets: UIEdgeInsets(top: -10, left: 0, bottom: 10, right: 0)),
createNavController(for: ViewController(), title: NSLocalizedString("settings", comment: ""), image: UIImage(named: "settings")!)
]
}
}
tabBarItem.image = tabBarImage.withRenderingMode(.alwaysOriginal)
试试这个解决方案
我想向 UITabBar 添加自定义图像,但它失去了颜色,我猜这是因为 tintColor。 What I get
What I want
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
UITabBar.appearance().backgroundColor = .menuColor
UITabBar.appearance().tintColor = .white
UITabBar.appearance().unselectedItemTintColor = .gray
setupVCs()
}
private func createNavController(for rootViewController: UIViewController,
title: String,
image: UIImage,
imageInsets: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) -> UIViewController {
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.title = title
navController.tabBarItem.image = image
navController.tabBarItem.imageInsets = imageInsets
navController.navigationBar.prefersLargeTitles = true
rootViewController.navigationItem.title = title
return navController
}
private func setupVCs() {
viewControllers = [
createNavController(for: ViewController(), title: NSLocalizedString("notes", comment: ""), image: UIImage(named: "notes")!),
createNavController(for: ViewController(), title: NSLocalizedString("", comment: ""), image: UIImage(named: "rec")!, imageInsets: UIEdgeInsets(top: -10, left: 0, bottom: 10, right: 0)),
createNavController(for: ViewController(), title: NSLocalizedString("settings", comment: ""), image: UIImage(named: "settings")!)
]
}
}
tabBarItem.image = tabBarImage.withRenderingMode(.alwaysOriginal)
试试这个解决方案