如何在iOS Swift中添加彩色图标?

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

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

gaolei3452 回答:如何在iOS Swift中添加彩色图标?

在x代码的资源文件夹中,选择您的图像,然后在属性检查器中,将渲染为的值更改为“原始图像” ,而不是“默认” 。

,

不要在情节提要中执行此操作。试试这个:

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")
,

这是因为在选项卡栏中,所有图像都以渲染模式设置为template的方式显示,因此可以在加载图像时覆盖此行为,从而强制使用渲染模式:

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

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

,

最好在这样的代码中执行此操作:

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
本文链接:https://www.f2er.com/3153847.html

大家都在问