【问题标题】:Custom tab bar background color. How to change color of tab bar background?自定义标签栏背景颜色。如何更改标签栏背景的颜色?
【发布时间】:2016-10-31 14:13:41
【问题描述】:

我想改变标签栏背景的颜色,我想把我自己的颜色加上颜色代码,怎么做?我有草图的颜色代码,不知道如何快速编码。

【问题讨论】:

  • 试试 navigationController?.toolbar.barTintColor = UIColor.green // 任何颜色
  • 我需要为标签栏创建一个子类吗?或者我可以把他的代码放在 appdelegate 中吗?
  • in viewDidLoad.....

标签: ios iphone swift


【解决方案1】:

在标签栏的 viewController 类中,将以下代码部分之一放入您的视图中,根据您拥有的值加载。

self.tabBar.barTintColor = UIColor.init(red: <#T##CGFloat#>, green: <#T##CGFloat#>, blue: <#T##CGFloat#>, alpha: <#T##CGFloat#>)

self.tabBar.barTintColor = UIColor.init(hue: <#T##CGFloat#>, saturation: <#T##CGFloat#>, brightness: <#T##CGFloat#>, alpha: <#T##CGFloat#>)

【讨论】:

  • 当我在 CustomTabBar 类中添加这段代码时,我得到了像“UIColor”没有成员“UIColor”这样的错误
  • 对不起,我错过了输入顶部的内容并两次输入 UIColor 应该只是 UIColor.init(),将编辑我的答案
【解决方案2】:

试试这个代码:

navigationController?.toolbar.barTintColor = UIColor.green // You can set to any colour.

【讨论】:

    【解决方案3】:

    您可以在AppDelegate.swift 中指定UITabBar 的外观:

    UITabBar.appearance().barTintColor = .white
    

    如果您想要使用十六进制代码自定义颜色的方法,您可以像这样向UIColor 添加扩展名:

    extension UIColor {
        convenience init (for hex: Int) {
            let red: Int =        (hex >> 16) & 0xff
            let green: Int =      (hex >> 8)  & 0xff
            let blue: Int =       hex         & 0xff
    
            assert(
                (red >= 0 && red <= 255) &&
                (green >= 0 && green <= 255) &&
                (blue >= 0 && blue <= 255),
                "bad hex for UIColor"
            )
    
            self.init (red: CGFloat(red) / 255.0, green: CGFloat (green) / 255.0, blue: CGFloat (blue) / 255.0, alpha: 1.0)
        }
    
        // Create your own custom color from hex code
        public class var customColor: UIColor {
            return UIColor (for: 0x0067DF)
        }
    }
    

    然后您可以将您的UITabBar 背景颜色更改为您自己的自定义颜色:

    UITabBar.appearance().barTintColor = .customColor
    

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 1970-01-01
      • 2018-06-13
      • 2022-01-26
      • 1970-01-01
      • 2019-02-08
      • 2016-01-14
      • 1970-01-01
      • 2023-01-08
      相关资源
      最近更新 更多