ios - 如何更改一个 View Controller 的选项卡栏的颜色?

标签 ios swift uitabbarcontroller

我的选项卡栏在 App Delegate 中有一个白色的条形色调,但我需要它在选定的索引之一上清晰可见。我将以下代码添加到需要清晰的 View Controller 中,但它保持清晰。我如何才能让它只适用于一个标签。

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

最佳答案

如果你有 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
    }
}

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

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

关于ios - 如何更改一个 View Controller 的选项卡栏的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43109363/

相关文章:

ios - AWS Dynamodb测试使用swift 3报错

iphone - 按下导航栏中的 "back"按钮时的自定义行为

ios - tabBar 减小嵌套 ViewController 中 View 的大小

ios - LayerKit 将附加信息设置为 LYRIdentity

swift - 快速获取 TableView 单元格内 uiswitch 值更改的标签值

ios - UIScrollView bug,有没有更好的选择?

ios - 如何识别 UITabBarController 跟随哪个 "relationship"?

iOS-charts 带有负值的条形图

ios - 将 URL 数据保存到变量

iphone - MFMailComposeViewController 的取消按钮(可能是操作表)卡住 View