Adding blur effect to background in swift

前端 未结 11 1966
鱼传尺愫
鱼传尺愫 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

    I have tested this code and it's working fine:

    let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(blurEffectView)
    

    For Swift 3.0:

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(blurEffectView)
    

    For Swift 4.0:

    let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(blurEffectView)
    

    Here you can see result:

    blurred view

    Or you can use this lib for that:

    https://github.com/FlexMonkey/Blurable

提交回复
热议问题