【问题标题】:How can I set the tabBarItems to different colours?如何将 tabBarItems 设置为不同的颜色?
【发布时间】:2020-04-23 11:56:31
【问题描述】:

我有一个 tabBar 设置,其中 5 个 tabBarItems 都进入不同的视图控制器。 我已经设置了项目,以便当未选择 tabBarItems 时,它在文本和图像上都是灰色调(我使用的是系统默认图像)。我正在寻找的是如何将每个选定的 tabBarItems 设置为不同的颜色。 EG,“Home”的文本和图像都会有红色调; “社交”的文本和图像都带有蓝色调; “关于”的文本和图像都会带有绿色;等等等等

【问题讨论】:

    标签: ios swift uitabbar uitabbaritem


    【解决方案1】:

    已回答 (Change tab bar item selected color in a storyboard) 但简而言之,单击您希望更改的选项卡栏项目,您可以在情节提要中添加一个新的运行时属性,该属性将在选择时更改整个项目(图像和文本)。可能对你有帮助。

    【讨论】:

    • 嗨,我已经尝试过,已为每个 tabBarItems 添加了 tintColor,但它们仍然具有相同的单一颜色(我可以更改,但不能单独更改)
    【解决方案2】:

    如果你定义了bar items,你可以这样做:

    让数组 = [UIColor.red, UIColor.blue, UIColor.green]

    实现UITabBarDelegate

    @IBOutlet weak var tab3: UITabBarItem!
    @IBOutlet weak var tab2: UITabBarItem!
    @IBOutlet weak var tab1: UITabBarItem!
    
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if item == tab1 {
            tabBar.tintColor = array[0]
        }else if item == tab2 {
            tabBar.tintColor = array[1]
        }else {
            tabBar.tintColor = array[2]
        }
    }
    

    如果您使用 TabBarController,请将此代码添加到每个 viewControllers:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.tintColor = .green // change the color for each viewControllers
    }
    

    【讨论】:

    • 嗨,谢谢你,我是 swift 新手——我应该在哪里添加这段代码?对每一个 ViewController 来说?
    • 嗨,我想你正在使用 tabBarController 所以我编辑了我的答案。
    • 太棒了!!谢谢你:)
    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多