UIVisualEffect未显示在UIView上

时间:2017-01-05 18:59:06

标签: ios iphone swift uiview

我有UIView并且以编程方式创建,然后我将subView添加到我的viewController.view。但是,视图不受模糊影响。 这是我的代码:

let blurryView: UIView = {
    let blur = UIView()
    blur.backgroundColor = UIColor.yellow
    blur.alpha = 0.90
    let blurEffect = UIBlurEffect(style: .extraLight)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blur.addSubview(blurEffectView)
    blurEffectView.center = blur.center
    blurEffectView.frame = blur.bounds
    blur.translatesAutoresizingMaskIntoConstraints = false
    return blur
}()

func manageBlurView() {
    blurryView.widthAnchor.constraint(equalToConstant: 250).isActive = true
    blurryView.heightAnchor.constraint(equalToConstant: 250).isActive = true
    blurryView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    blurryView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(blurryView)
    manageBlurView()

}
  • 添加了模糊效果,我加倍检查使用breakpoints和品脱语句if blur.subviews == [blurEffectView] { print("Blur effect added as a subview") }并打印出来。
  • 我有一张背景图片,以确保它是否模糊。
  • 我还试图在viewDidLoad()中添加模糊效果,但没有发生任何事情。

1 个答案:

答案 0 :(得分:1)

将黄色背景视图和模糊视图直接添加为主视图的子视图:

    let backg = UIView(frame: self.view.frame)
    backg.backgroundColor = UIColor.yellow
    backg.alpha = 0.90

    self.view.addSubview(backg)

    // the blur effect
    let blurEffect = UIBlurEffect(style: .extraLight)
    let blurredEffectView = UIVisualEffectView(effect: blurEffect)

    blurredEffectView.frame = self.view.frame
    self.view.addSubview(blurredEffectView)

您可以将上述代码放在viewDidLoad