ios - View 上的模糊背景无法始终如一地工作

标签 ios swift background viewcontroller uiblureffect

使用以下代码使 View 的背景变得模糊无法始终如一地工作。

func makeBackgroundBlurry () {
    var blurEffect = UIBlurEffect()
    blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds //view is self.view in a UIViewController
    view.insertSubview(blurEffectView, atIndex: 0)
    view.backgroundColor = UIColor.clearColor()


    //add auto layout constraints so that the blur fills the screen upon rotating device
    blurEffectView.setTranslatesAutoresizingMaskIntoConstraints(false)
    view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
}

背景模糊被应用于模态呈现的 View Controller ,如下所示:

@IBAction func editTitleButtonAction(sender: UIButton) {
        let vc = FieldEditor(nibName: "FieldEditor", bundle: nil)
        vc.labelOne = "Make / Manufacturer"
        vc.valueOne = item.manufacturer
        vc.labelTwo = "Model / Item Name"
        vc.valueTwo = item.name
        vc.delegate = self
        self.presentViewController(vc, animated: true, completion:nil)

    }

偶尔,它会按预期工作,在背景上产生光模糊效果;然而,大多数时候背景是模糊的,但是呈现当前 View 的 View 在下面是不可见的。事实上,它模糊了黑色背景。但是,如果 View 的呈现是动画,则 View 仅在动画期间以适当的模糊背景呈现。

我也试过这样展示 View ,但无济于事:

self.presentViewController(vc, animated: true, completion:{vc.makeBackgroundBlurry()})

如何解决这个问题,以便模态呈现的 View 在呈现期间和完成时都具有模糊的背景?

最佳答案

the view which presented the current view is not visible underneath

这是您应该期待的。当您呈现一个 View Controller 时,呈现 View Controller 的 View 默认情况下会消失。呈现的 View Controller 的 View 背后没有任何内容。那是正常的。因此,您会在半透明 View Controller 的 View 后面看到黑色的窗口。

但是,在 iOS 8 中,您可以通过使用 .OverFullScreen 作为呈现的 View Controller 的模态呈现样式来防止呈现的 View Controller 的 View 消失:

 vc.modalPresentationStyle = .OverFullScreen

关于ios - View 上的模糊背景无法始终如一地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30159787/

相关文章:

ios - UINavigationBar 底部边框消失

android - 将背景图像应用于 AlertDialog

css - IE 8 PNG正文背景问题

css: webkit-input-placeholder 背景色

iOS 9 推送通知 dyld`dyld_fatal_error :

ios - 使用 Swift 3 调整/添加定时器输出

ios - iOS-GameKit获取正在进行的比赛列表?

ios - `indexOf` 抛出 "Object type does not match RLMResults"异常

ios - 编译适用于 iOS 6 和 iOS 7 的应用程序

ios - 在 SwiftUI 中呈现 NavigationLink 时过渡动画消失了