【问题标题】:How do I change the color of the tab bar for one view controller?如何更改一个视图控制器的标签栏颜色?
【发布时间】:2017-08-23 21:02:39
【问题描述】:

我的标签栏的应用程序委托中有一个白色的栏色调,但我需要它在所选索引之一上清晰。我将以下代码添加到需要清晰的视图控制器中,但它保持清晰。我如何拥有它,所以这只适用于一个标签。

let tabBar = self.tabBarController?.tabBar
tabBar?.barTintColor = UIColor.clear
tabBar?.backgroundImage = UIImage()
tabBar?.shadowImage = UIImage()

【问题讨论】:

  • 你有 tabBarController 类吗?
  • 是的,我尝试设置视图中选定的索引是否加载
  • 检查我的答案,使用委托
  • 对于透明 tabBar 检查这个答案。 stackoverflow.com/a/61815508/7301439

标签: ios swift uitabbarcontroller


【解决方案1】:

如果你有 UITabBarControllerDelegate 你可以使用

func tabBarController(_ tabBarController: UITabBarController,
                      didSelect viewController: UIViewController) {

    guard let index = self.viewControllers?.index(where: { $0 == viewController }) else {
        return
    }
    if index == 1 {
        self.tabBar.barTintColor = .black
    } else {
        self.tabBar.barTintColor = .yellow
    }
}

我正在按索引检查,因为我有导航控制器,这对我来说更容易,你可以尝试使用

if viewController is MyViewController {
    self.tabBar.barTintColor = .black
} else {
    self.tabBar.barTintColor = .yellow
}

【讨论】:

    【解决方案2】:

    试试这个。在一个选项卡上设置 UIColor.clear 并在另一个选项卡上设置不同的颜色。

    let tabBar = self.tabBarController?.tabBar
    let tabBarItems = tabBar?.items
    if(tabBar?.selectedItem == tabBarItems?[0])
    {
        tabBar?.barTintColor = UIColor.clear
    }
    else
    {
        tabBar?.barTintColor = UIColor.red
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-14
      • 2012-04-07
      • 2020-07-13
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      相关资源
      最近更新 更多