我有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()
中添加模糊效果,但没有发生任何事情。答案 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
。