Ios UItabaritem默认颜色
您好,我想将未选择的bur项目颜色更改为白色,我编写了以下代码:Ios UItabaritem默认颜色,ios,swift,uitabbar,Ios,Swift,Uitabbar,您好,我想将未选择的bur项目颜色更改为白色,我编写了以下代码: for item in self.tabBar.items as [UITabBarItem]! { if let image = item.image { item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal) } } 但我在第一
for item in self.tabBar.items as [UITabBarItem]! {
if let image = item.image {
item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
但我在第一行给了我这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我该怎么办?这里有一个最简单的代码示例,对我来说很有用
class MyTabBarController : UITabBarController {
override func viewDidLoad() {
if let items = self.tabBar.items {
for item : UITabBarItem in items {
if let image = item.image {
item.image = getImageWithColor(UIColor.whiteColor(), size: image.size).imageWithRenderingMode(.AlwaysOriginal)
}
}
}
}
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
getImageWithColor
函数取自。如果您还希望所选选项卡为白色正方形,请设置项。同时选择图像属性。以下是一个适用于我的最小代码示例
class MyTabBarController : UITabBarController {
override func viewDidLoad() {
if let items = self.tabBar.items {
for item : UITabBarItem in items {
if let image = item.image {
item.image = getImageWithColor(UIColor.whiteColor(), size: image.size).imageWithRenderingMode(.AlwaysOriginal)
}
}
}
}
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
getImageWithColor
函数取自。如果您还希望所选选项卡为白色正方形,请设置项。同时选择图像属性。您是使用自定义图像作为选项卡栏项目图标还是系统图像?请用更多代码展开您的问题。如果有tabBar,像“for item in self.tabBar.items!{}”这样的简单迭代在标准UITabBarController中运行良好items@Ishanhanda不,我用的是定制的images@peacer212是的,我知道,它曾经工作过,但当我添加本地通知时,它停止了工作。我不知道为什么,因为这两个完全不相关。我真的帮不了你,因为我不能重现错误。您能用一个更完整的代码示例来说明吗?您是使用自定义图像作为选项卡栏项目图标还是系统图像?请用更多代码来展开您的问题。如果有tabBar,像“for item in self.tabBar.items!{}”这样的简单迭代在标准UITabBarController中运行良好items@Ishanhanda不,我用的是定制的images@peacer212是的,我知道,它曾经工作过,但当我添加本地通知时,它停止了工作。我不知道为什么,因为这两个完全不相关。我真的帮不了你,因为我不能重现错误。你能不能用一个更完整的代码示例来做一个比较好的例子呢?这个比较好用,但是它制作了一个白色的框,而不是把图标改成白色。我修正了它。我稍微改变了一下功能,现在它工作正常了。谢谢。我很高兴它能这样工作。你能更新你的问题吗?你在将来的参考资料中使用的代码?这有点正确,但它制作了一个白色框,而不是将图标更改为白色OK我修复了它。我稍微改变了一下功能,现在它工作正常了。谢谢。我很高兴它能这样工作。你能把你的问题更新为你将来参考的代码吗?