我正在尝试为视图添加模糊效果。我像这样创建了模糊效果

let blurLabel: UIVisualEffectView = {
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)


    return blurEffectView
    }()


然后像这样将其添加到子视图中,并设置约束

 override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
        let view = tableViewCell

        addSubview(view)
        view.addSubview(blurLabel)

 blurLabel.leftAnchor.constraint(equalTo: self.leftAnchor,constant:0).isActive = true
        //blurLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 178).isActive = true
        blurLabel.widthAnchor.constraint(equalToConstant: 375).isActive = true
        blurLabel.heightAnchor.constraint(equalToConstant: 180).isActive = true


但是当我运行该应用程序时,它根本没有出现

最佳答案

编辑:根据@HAS和@Fogmeister的建议,您也可以使用translatesAutoResizingMaskIntoConstraints = false,因为它是使用自动布局而不指定显式框架的更好解决方案。

您必须像这样将帧分配给UIVisualEffectView

 let blurLabel: UIVisualEffectView = {
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = CGRect(x: 0, y: 0, width: 60, height: 40) //give desirable frame
    return blurEffectView
 }()

10-06 02:56