Property
// 默认为"true",建议设置为"false"
self.tabBar.isTranslucent = false
// 默认即为 "白色"
self.tabBar.barTintColor = .white
- 应用于
tab
选项卡栏项的选中时颜色(image & title)
// 同时设置选项卡选中时,图片颜色和标签颜色
self.tabBar.tintColor = .orange
// 设置选项卡选中时,图片为UI设计时原图样式,而非系统提供的模板样式
vc.tabBarItem.selectedImage = UIImage(named: selectedImgName)?.withRenderingMode(.alwaysOriginal)
// ⚠️⚠️⚠️若要同时设置"normal"状态与"selected"状态文本样式,则务必设置"self.tabBar.barTintColor"属性
vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)
Example
import UIKit
class CCTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.setUI()
}
// MARK: 初始化"CCTabBarController"
func setUI() {
self.tabBar.isTranslucent = false
self.tabBar.barTintColor = .white
// home
self.addChildController(vc: CCNavigationViewController(rootViewController: CCHomeViewController()), title: "首页", normalImgName: "tab_home_normal", selectedImgName: "tab_home_seleted")
// user
self.addChildController(vc: CCNavigationViewController(rootViewController: CCUserViewController()), title: "我的", normalImgName: "tab_user_normal", selectedImgName: "tab_user_seleted")
}
// MARK: 添加子控制器
private func addChildController(vc: UIViewController, title: String, normalImgName: String, selectedImgName: String) {
vc.tabBarItem.title = title
vc.tabBarItem.image = UIImage(named: normalImgName)?.withRenderingMode(.alwaysOriginal)
vc.tabBarItem.selectedImage = UIImage(named: selectedImgName)?.withRenderingMode(.alwaysOriginal)
// ⚠️⚠️⚠️ 若要同时设置"normal"状态与"selected"状态,则务必设置"self.tabBar.barTintColor"属性
vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)
addChild(vc)
}
}
网友评论