我在 viewDidLoad() 中的行提示没有 !在以下声明中:
view.backgroundColor = UIColor.colorWithPatternImage(UIImage(named: "background.jpg"))
但是,当我按照建议将其更改为:
view.backgroundColor = UIColor.colorWithPatternImage(UIImage(named: "background.jpg")!)
它给出了以下错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
再次强调,我们将不胜感激!
最佳答案
将您的代码更改为:
if let image = UIImage(named: "background.jpg") {
view.backgroundColor = UIColor.colorWithPatternImage(image)
} else {
println("There was no such image as background.jpg")
}
这样你就不会崩溃,println
将记录到控制台并向你证明图像不存在 - 当然,直到它存在。
关于ios - Swift:将 UIImage 设置为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29885003/