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

5

我有一个小问题:我想在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等全部消失了。有人能帮忙吗?

非常感谢!


1
写一个 view.insertSubView(blurView, atIndex: 0) 怎么样? - Epic Defeater
你能解决这个问题吗? - Epic Defeater
3个回答

9
尝试使用view.insertSubView(blurView, atIndex: 0)代替view.addSubview(blurView) as! UIImageView

哈哈哈哈,你真是个天才,或者我太蠢了XD,非常感谢你,晚安(我是意大利人)! - Fabio Cenni
1
在我的情况下,self.tableView.backgroundView!.addSubview(blurView) 适用于我,而不是 view.insertSubView(blurView, atIndex: 0) - cdf1982

2

这个问题是一个较旧的问题,Epic Defeater的回答在某种程度上是可以的,但有一种更好的方法来实现这个目标。你需要覆盖背景视图,而不是添加blurView。具体来说,你需要覆盖tableView.backgroundView

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

这只有在您将背景颜色设置为透明时才能起作用。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接