美文网首页
(Swift) UITabBarController常用属性

(Swift) UITabBarController常用属性

作者: 布呐呐u | 来源:发表于2021-05-27 22:51 被阅读0次

Property

  • 一个布尔值,用于指明tab选项卡栏是否半透明
// 默认为"true",建议设置为"false"
self.tabBar.isTranslucent = false
  • 应用于tab选项卡栏背景的着色颜色
// 默认即为 "白色"
self.tabBar.barTintColor = .white
  • 应用于tab选项卡栏项的选中时颜色(image & title)
// 同时设置选项卡选中时,图片颜色和标签颜色
self.tabBar.tintColor = .orange
  • 设置tab选项卡栏项选中时显示原图而非模板图
// 设置选项卡选中时,图片为UI设计时原图样式,而非系统提供的模板样式
vc.tabBarItem.selectedImage = UIImage(named: selectedImgName)?.withRenderingMode(.alwaysOriginal)

  • 设置tab选项卡栏项标题的文本属性
// ⚠️⚠️⚠️若要同时设置"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)
    }
}

相关文章

网友评论

      本文标题:(Swift) UITabBarController常用属性

      本文链接:https://www.haomeiwen.com/subject/qwvdsltx.html