【问题标题】:how to add colored icon in ios swift?如何在 ios swift 中添加彩色图标?
【发布时间】:2020-03-02 16:43:51
【问题描述】:

我正在尝试添加一个多色的图标,当我在标签栏中添加图标时,它显示为单色蓝色,图标的实际颜色不可见?

如何在标签栏中添加彩色图标?

【问题讨论】:

标签: ios swift icons uitabbaritem


【解决方案1】:

在 x-code 的 assets 文件夹中选择您的图像并在属性检查器中将 Render As 的值更改为 "Original Image" 而不是 "Default"。

【讨论】:

    【解决方案2】:

    这是因为在标签栏中,所有图像都显示为渲染模式设置为template,您可以在加载图像时覆盖此行为强制渲染模式:

    let yourImage = UIImage(named: "your_image")?.withRenderingMode(.alwaysOriginal)
    

    然后将您的图像用作标签栏图标。

    【讨论】:

      【解决方案3】:

      最好在这样的代码中这样做:

      var aViewController: UIViewController = UIViewController()
      

      //这个语句就是你需要的

      var myTabBarItem: UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
      aViewController.tabBarItem = myTabBarItem
      

      【讨论】:

        【解决方案4】:

        不要在情节提要中这样做。试试这个:

        extension UITabBarItem {
        
            convenience init(title: String, unselected: String, selected: String) {
        
                let selectedImage = UIImage(named: selected)?.withRenderingMode(.alwaysOriginal)
                let unselectedImage = UIImage(named: unselected)?.withRenderingMode(.alwaysOriginal)
        
                self.init(title: title, image: unselectedImage, selectedImage: selectedImage)
            }
        }
        

        然后在您的视图控制器中的viewDidLoad...

        tabBarItem = UITabBarItem(title: "My title",
                                  unselected: "unselectedIconName",
                                  selected: "selectedIconName")
        

        【讨论】:

          猜你喜欢
          • 2018-06-02
          • 1970-01-01
          • 2014-09-24
          • 2019-07-28
          • 1970-01-01
          • 2019-04-04
          • 1970-01-01
          • 2015-11-07
          • 1970-01-01
          相关资源
          最近更新 更多