TableView with image background and blur effect - swift
Asked Answered
Y

3

5

I have a little problem: I'd like to set an image to background in a TableViewController. I tried this:

class SquadreController: UITableViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()

        var sfondo = UIImage(named: "sfondo")
        self.view.backgroundColor = UIColor(patternImage: sfondo!)

    }

Then I give the blur effect to the view, like this:

let blur = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = self.view.bounds
view.addSubview(blurView)

But there is a big problem. I'm working in a TableView and the TableView, TableCell, NavigationItem, etc. have all disappeared after I apply the blur. Can someone help me?

Thanks a lot!

Yim answered 28/7, 2015 at 23:51 Comment(2)
How about writing view.insertSubView(blurView, atIndex: 0)Sola
Were you able to fix the problem?Sola
S
9

Try using view.insertSubView(blurView, atIndex: 0) instead of view.addSubview(blurView) as! UIImageView

Sola answered 29/7, 2015 at 0:30 Comment(2)
ahahahah you are a genius, or I'm a stupid XD thanks a lot guy, and Goodnight (I'm italian)!!!Yim
In my case, self.tableView.backgroundView!.addSubview(blurView) did the trick instead of view.insertSubView(blurView, atIndex: 0)Daughterinlaw
N
2

This question is an older question and Epic Defeater's response does in a way work, there is a much better way to accomplish this. Instead of adding blurView, you need to override the background view. Specifically you need to override tableView.backgroundView.

let blur = UIBlurEffect(style: .light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = self.view.bounds
tableView.backgroundView = blurView

This will only work if you have set the background color to clear.

Neuroticism answered 15/12, 2017 at 0:27 Comment(0)
L
0

We can also add a subview below or above any other views using insertSubview

//Inserting below your subView
self.view.insertSubview(subview1_name, belowSubview: subview2_name)

//Inserting above your subView
self.view.insertSubview(subview1_name, aboveSubview: subview2_name)

/// You can use any of your view, instead of self.view
Legionary answered 3/3, 2017 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.