5

I want to have blur effect as background for my View, as we know, we can blur the view and what is inside, but I do't want blur content of view but I want blur background view to give a glassy blurred through effect as we know. How i can do this in SwiftUi?

In other words, the under layer of view would be blurred in frame on current view which sets as background for current view.

ios coder
  • 1
  • 4
  • 31
  • 91
  • Does this answer your question https://stackoverflow.com/a/59527255/12299030? Or in this way https://stackoverflow.com/a/64301261/12299030? – Asperi Nov 27 '20 at 15:03
  • Do you mean to have the look like the image you posted? If so, that is a shadow, not a blur. Does this help?: [https://www.hackingwithswift.com/quick-start/swiftui/how-to-draw-a-shadow-around-a-view](https://www.hackingwithswift.com/quick-start/swiftui/how-to-draw-a-shadow-around-a-view) – Yrb Nov 27 '20 at 16:13

2 Answers2

3

In iOS 15 you can use Materials, like .ultraThinMaterial

.background(.ultraThinMaterial)

But from iOS 14 down, there is no SwiftUI way to achieve this, though it is possible to create an UIViewRepresentable and place it as a background.

Following code will create a UIViewRepresentable that you are able to use as a background: .systemUltraThinMaterial can be changed to any material

import SwiftUI
struct BlurView: UIViewRepresentable {
    var style: UIBlurEffect.Style = .systemUltraThinMaterial
    func makeUIView(context: Context) -> UIVisualEffectView {
        return UIVisualEffectView(effect: UIBlurEffect(style: style))
    }
    func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
        uiView.effect = UIBlurEffect(style: style)
    }
}

View extension (optional):

extension View {
    func backgroundBlurEffect() -> some View {
        self.background(BlurView())
    }
}

Then use on a view like a pop up.

.background(BlurView()) 

or if used like extension

.backgroundBlurEffect()

See image

NoosaMaan
  • 130
  • 11
0

You can just use

.blur(radius: showCustomAlertView ? 5 : 0)//<< here comes your blur

on your view to blur the view.

davidev
  • 7,694
  • 5
  • 21
  • 56