Marcel Marcel - 1 month ago 5
iOS Question

Background image not loading on device

I have this code that places the image in the background and applies a blur effect:

let effect = UIBlurEffect(style: .Dark)
override func viewDidLoad() {
let backgroundView = UIView(frame: view.bounds)
backgroundView.autoresizingMask = resizingMask
backgroundView.addSubview(self.buildImageView())
backgroundView.addSubview(self.buildBlurView())
tableView.backgroundColor = UIColor.clearColor()
tableView.backgroundView = backgroundView
tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)


}
func buildImageView() -> UIImageView {
let imageView = UIImageView(image: UIImage(named: "pexels-photo"))
imageView.frame = view.bounds
imageView.autoresizingMask = resizingMask
return imageView
}

func buildBlurView() -> UIVisualEffectView {
let blurView = UIVisualEffectView(effect: effect)
blurView.frame = view.bounds
blurView.autoresizingMask = resizingMask
return blurView
}


When I run this in the simulator it loads fine. But when I test it on an iPhone 6 it just loads a black background.

Any help on how to solve this problem would be appreciated.

Joe Joe
Answer

Try below code tested in Xcode 8 it worked. Code tested as a UIView..

  **Answer 1:** From your code
   // Change bLurView Style to .dark or .extraLight If you want 
    let effect = UIBlurEffect(style: .light)

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let backgroundView = UIView(frame: view.bounds)
    // backgroundView.autoresizingMask = resizingMask
    view.addSubview(self.buildImageView())
    view.addSubview(self.buildBlurView())
    /////Code tested in UIView not in tableView.It won't do any difference just let you know///////
    tableView.backgroundColor = UIColor.clearColor()
    // tableView.backgroundView = backgroundView
    tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)
    /////////////////////////////////////////////////////////////////////  
    }

    func buildImageView() -> UIImageView {
        let imageView = UIImageView(image: UIImage(named: "pexels-photo"))
        imageView.frame = view.bounds
       // imageView.autoresizingMask = resizingMask
        return imageView
    }

    func buildBlurView() -> UIVisualEffectView {
        let blurView = UIVisualEffectView(effect: effect)
        blurView.frame = view.bounds
     //   blurView.autoresizingMask = resizingMask
        return blurView
    }

Answer 2:

    override func viewDidLoad() {
    super.viewDidLoad()
    // Add a background view to the table view
    let backgroundImage = UIImage(named: "your image file")
    let imageView = UIImageView(image: backgroundImage)
    self.tableView.backgroundView = imageView
    imageView.contentMode = .ScaleAspectFit 


    // no lines where there aren't cells
    tableView.tableFooterView = UIView(frame: CGRectZero)

    //set background colour to light color or clear color to get a transparent look
    tableView.backgroundColor = .lightGrayColor()

     let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
     let blurView = UIVisualEffectView(effect: blurEffect)
     blurView.frame = imageView.bounds
     imageView.addSubview(blurView)

     }

    If we want to make the table view cells totally transparent we can just  set their background color to clear:

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
   cell.backgroundColor =  UIColor(white: 1, alpha: 0.5) // UIcolor.clear
   }

output of the above code as below....

enter image description here

Comments