Mario Burga
Mario Burga

Reputation: 1157

UITableView blurred background

I have a UITableViewController (static table) to which I add a background image with (in viewDidLoad):

tableView.backgroundView = UIImageView(image: UIImage(named: "login_bg.jpg"))

How do I add the blurred effect to that background image?

I tried with: (in viewDidLoad)

    let blurEffect = UIBlurEffect(style: .light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)

    blurEffectView.frame = self.view.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    self.tableView.backgroundView = blurEffectView

It works but it does not catch the background image.

Upvotes: 1

Views: 92

Answers (1)

Pooja Kamath
Pooja Kamath

Reputation: 1298

You need to add the blur effect view as a subview instead of setting it as the background view.

    self.tableView.backgroundView?.addSubview(blurEffectView)

Step 1 :

Set the background image.

 tableView.backgroundView = UIImageView(image: UIImage(named: "img"))

Step 2:

Create blur effect view and add it as subview.

        let blurEffect = UIBlurEffect(style: .light)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)

        blurEffectView.frame = self.view.bounds
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        self.tableView.backgroundView?.addSubview(blurEffectView)

Here is how it looks.enter image description here

Upvotes: 2

Related Questions