Gaurav Vyas Gaurav Vyas - 6 months ago 17
Swift Question

Getting error When set background image on UIView

Getting error: unexpectedly found nil while unwrapping an Optional value
(lldb)

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var upperBackgroundView: UIView! // Interface builder view

override func viewDidLoad() {
super.viewDidLoad()

upperBackgroundView.backgroundColor = UIColor(patternImage: UIImage(named:"image.png")!);
}
}

Answer

May be image not found. Try this

  if let img = UIImage(named:"image.png") {
      upperBackgroundView.backgroundColor = UIColor(patternImage: img);
   }else{
      print("Image not found..")
   }