【问题标题】:Blur effect not appearing in UITableViewCellUITableViewCell 中没有出现模糊效果
【发布时间】:2017-03-03 05:02:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios swift uitableview uivisualeffectview uiblureffect


    【解决方案1】:

    编辑:正如@HAS 和@Fogmeister 所建议的,您也可以使用translatesAutoResizingMaskIntoConstraints = false,因为在不指定显式框架的情况下使用自动布局是更好的解决方案。

    您必须像这样将 frame 分配给 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
     }()
    

    【讨论】:

    • 这应该没有必要。由于您已经使用自动布局,您应该能够设置模糊视图的translatesAutoResizingMaskIntoConstraints = false,以便自动布局不会尝试将其宽度和高度设为零(因为您没有使用框架对其进行初始化,所以它假定框架为{0, 0, 0, 0})。正如 Arpit 所说,一种可能性是设置一个新框架,但我认为更简洁的解决方案是使用没有显式框架的自动布局。
    • 是的,@HAS 在这里是正确的。你需要做的是设置translatesAutoResizingMaskIntoConstraints = false。哦,是的,他已经说过了:D
    【解决方案2】:

    如果您愿意,您也可以将您的 blurView 提供给您的任何视图或子视图,而不是提供明确的框架。

    let blurLabel: UIVisualEffectView = {
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)    
        blurEffectView.frame = myMainview.bounds //Blur effect's frame
        return blurEffectView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Use it somewhere using
        self.view.insertSubview(blurEffectView, belowSubview: myAnotherView)
        // You may also use any of the subviews too, instead of the self.view
        /// self.myView.insertSubview(blurEffectView, belowSubview: myAnotherView)
    }
    

    【讨论】:

      【解决方案3】:

      要解决此问题,您只需停止它自动创建约束。像这样……

      let blurLabel: UIVisualEffectView = {
          let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
          let blurEffectView = UIVisualEffectView(effect: blurEffect)
          // this line will stop constraints being added for a zero frame
          blurEffectView.translatesAutoResizingMaskIntoConstraints = false
          return blurEffectView
      }()
      

      【讨论】: