我正在尝试在自定义alertView中插入tableview。我需要实现与默认警报相同的样式。从调试视图层次结构中,我几乎复制了样式,但我不知道苹果是如何设置模糊效果的。我的自定义警报视图层次结构here
我试图以编程方式插入模糊视图,但结果太暗、太白或透明度相同
let blur = UIVisualEffectView(effect: UIBlurEffect(style: .all of them))
blur.frame = self.alertView.frame
blur.isUserInteractionEnabled = false
self.view.insertSubview(blur, at: 1)
最佳答案
它们有几个不同的对话框,但这是一个很好的起点:
/// Composite view of blur + vibrancy + white bg color blending
/// which recreates the Apple alert dialog effect
lazy var dialogView: UIVisualEffectView = {
let blur = UIBlurEffect(style: .regular)
let blurView = UIVisualEffectView(effect: blur)
let vibrancy = UIVibrancyEffect(blurEffect: blur)
let vibrantView = UIVisualEffectView(effect: blurView.effect)
vibrantView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
blurView.contentView.addSubview(vibrantView)
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
blurView.layer.cornerRadius = 20
blurView.layer.masksToBounds = true
let blendView = UIView()
blendView.backgroundColor = .white
blendView.alpha = 0.5
blendView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
blurView.contentView.addSubview(blendView)
return blurView
}()
然后将其添加到视图中并约束w/autolayout。