quanshousio / ToastUI

A simple way to show toast in SwiftUI.
MIT License
578 stars 45 forks source link

Can I choose to not have background? #8

Closed wyk111wyk closed 4 years ago

wyk111wyk commented 4 years ago

Pre-requisites:

Feature Suggestion

Possible Implementation

Context

quanshousio commented 4 years ago

Yes, you can. As demonstrated in CustomizedToastWithoutToastViewExample, you just need to provide your SwiftUI views directly without using ToastView. Here's a snippet from that example using Swift 5.3+.

struct CustomizedToastWithoutToastViewExample: View {
  @State private var presentingToast: Bool = false

  var body: some View {
    Button {
      presentingToast = true
    } label: {
      Text("Tap me")
    }
    .toast(isPresented: $presentingToast, dismissAfter: 2.0) {
      print("Toast dismissed")
    } content: {
      // your SwiftUI views here
      VStack {
        Spacer()
        Text("You can put any SwiftUI views here without the need of ToastView")
          .bold()
          .foregroundColor(.white)
          .padding()
          .background(Color.green)
          .cornerRadius(8.0)
          .shadow(radius: 4)
          .padding()
      }
    }
  }
}
wyk111wyk commented 4 years ago

But this is for pure Text, can't remove the blur background of success or progress view(ToastView)

quanshousio commented 4 years ago

ToastView hasn't supported styles without background yet. Thank you for the suggestion and I will push an update in the near future.

quanshousio commented 4 years ago

Sorry for the late update but ToastUI has just been updated and you can now use ToastView with custom background (#11). Here's an example of using ToastView with IndefiniteProgressToastViewStyle but without blur background.

.toast(isPresented: $presentingToast) {
  ToastView("Loading...") {
    // EmptyView()
  } background: {
    // EmptyView()
  }
  .toastViewStyle(IndefiniteProgressToastViewStyle())
}

Passing EmptyView is optional so you can reduce it to be one-liner.

.toast(isPresented: $presentingToast, dismissAfter: 2.0) {
  ToastView("Loading...") {} background: {}
    .toastViewStyle(IndefiniteProgressToastViewStyle())
}

The first closure is to provide your custom content view just like before and the second closure is for custom background view. For more information, check out the new ToastViewWithCustomBackgroundExample in ToastUISample.