Adding blur effect to background in swift

前端 未结 11 1938
鱼传尺愫
鱼传尺愫 2020-11-29 14:50

I am setting a background image to view controller. But also i want to add blur effect to this background. How can I do this?

I am setting background with following

11条回答
  •  渐次进展
    2020-11-29 15:32

    This one always keeps the right frame:

    public extension UIView {
    
        @discardableResult
        public func addBlur(style: UIBlurEffect.Style = .extraLight) -> UIVisualEffectView {
            let blurEffect = UIBlurEffect(style: style)
            let blurBackground = UIVisualEffectView(effect: blurEffect)
            addSubview(blurBackground)
            blurBackground.translatesAutoresizingMaskIntoConstraints = false
            blurBackground.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
            blurBackground.topAnchor.constraint(equalTo: topAnchor).isActive = true
            blurBackground.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
            blurBackground.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
            return blurBackground
        }
    }
    

提交回复
热议问题