UIVisualEffectView无法在UIWebView上方运行

时间:2016-03-19 07:59:18

标签: ios swift uiwebview blur uivisualeffectview

我在UIVisualEffectView内有一个myView。可以在屏幕上拖动myView。此外,ViewController中还有一个图像和UIWebView 在图像上方一切正常但是当我将myView拖到UIWebView上方时,模糊效果不再起作用,myView看起来像是浅灰色背景并且是透明的。

[来自真实iPad Air的结果,iOS 9.2.1]

working blur effect
工作(UIImage以上)

not working blur effect
无法正常工作(UIWebView以上)

这就是我创建模糊效果的方法:

let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .ExtraLight))
blurView.frame.size = CGSizeMake(navView.frame.width, navView.frame.height)
blurView.layer.cornerRadius = navView.layer.cornerRadius
blurView.clipsToBounds = true

let vibrancyView = UIVisualEffectView()
vibrancyView.frame = blurView.frame
vibrancyView.effect = UIVibrancyEffect(forBlurEffect: blurView.effect as! UIBlurEffect)
vibrancyView.layer.cornerRadius = blurView.layer.cornerRadius

blurView.contentView.addSubview(vibrancyView)
vibrancyView.contentView.addSubview(left)
vibrancyView.contentView.addSubview(right)
myView.addSubview(blurView)

知道为什么会这样吗? 提前谢谢!

2 个答案:

答案 0 :(得分:2)

添加此内容并尝试

myView.bringSubviewToFront(blurView)

或使用

blurView.superview.bringSubviewToFront(blurView)

<强>更新

变量创建

@IBOutlet var webview: UIWebView!

  let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .ExtraLight))
    blurView.frame.size = CGSizeMake(200, 200)
    blurView.layer.cornerRadius = 25
    blurView.clipsToBounds = true

    let vibrancyView = UIVisualEffectView()
    vibrancyView.frame = blurView.frame
    vibrancyView.effect = UIVibrancyEffect(forBlurEffect: blurView.effect as! UIBlurEffect)
    vibrancyView.layer.cornerRadius = blurView.layer.cornerRadius

    blurView.contentView.addSubview(vibrancyView)
   // vibrancyView.contentView.addSubview(left)
    //vibrancyView.contentView.addSubview(right)
    webview.addSubview(blurView)

    let myURL = NSURL(string: "http://www.swiftdeveloperblog.com");
    let myURLRequest:NSURLRequest = NSURLRequest(URL: myURL!);
    webview.loadRequest(myURLRequest);

enter image description here

我添加了Sample project

答案 1 :(得分:0)

试试这个

self.view.sendSubviewToBack(UIWebView)