具有图像背景和模糊效果的 TableView - swift

TableView with image background and blur effect - swift

我有一个小问题:我想在 TableViewController 中将图像设置为背景。我试过这个:

class SquadreController: UITableViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()

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

    }

然后我给视图添加模糊效果,像这样:

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

但是有个大问题。我在 TableView 中工作,TableView、TableCell、NavigationItem 等在我应用模糊后都消失了。有人可以帮助我吗?

非常感谢!

尝试使用 view.insertSubView(blurView, atIndex: 0) 而不是 view.addSubview(blurView) as! UIImageView

我们还可以使用 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

这个问题是一个较旧的问题,Epic Defeater 的回答在某种程度上是有效的,有更好的方法来完成这个问题。您需要覆盖背景视图,而不是添加 blurView。具体来说,您需要覆盖 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.