4

I'd like to use this project Toast-Swift through CocoaPods in a SwiftUI view. It's for UIView, so I tried to write a ViewController and wrap it into SwiftUI but the result is nothing on the screen.

My code:

struct ToastView: UIViewControllerRepresentable{

    @State var text: String

    func makeUIViewController(context: UIViewControllerRepresentableContext<ToastView>) -> UIToast {
        return UIToast(text: text)
    }

    func updateUIViewController(_ uiViewController: UIToast, context: UIViewControllerRepresentableContext<ToastView>) {

    }
}

class UIToast: UIViewController{

    var text: String = ""

    init(text: String) {
        super.init(nibName: nil, bundle: nil)
        self.text = text
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.makeToast(text)
    }
}

I've found some custom implementations on SO of a Toast for SwiftUI (SO question) but their behaviour is not exactly what I was looking for.

Can someone please help me to fix this? Is there another recommendation for Toast in SwiftUI? Thanks in advance!

zampnrs
  • 345
  • 1
  • 2
  • 13

4 Answers4

8

I put it here for anyone who still looking for this subject using SwiftUI:

https://github.com/elai950/AlertToast

struct ContentView: View{

    @State private var showAlert = false

    var body: some View{
        VStack{

            Button("Show Alert"){
                 showAlert.toggle()
            }
        }
        .toast(isPresenting: $showAlert){

            // `.alert` is the default displayMode
            AlertToast(displayMode: .alert, type: .regular, title: "Message Sent!")
            
            //Choose .hud to toast alert from the top of the screen
            //AlertToast(displayMode: .hud, type: .regular, title: "Message Sent!")
        }
    }
}
Eli
  • 181
  • 3
  • 12
3

I don´t know if you have resolved your problem, but I´m posting this here in case someone is interested. I managed to do this getting a reference to the SceneManager, and then using its rootViewController view. With this view you can use Toast_Swift Example:

Button(action: {
                 let scene = UIApplication.shared.connectedScenes.first
                 if let sceneDelegate : SceneDelegate = scene?.delegate as? SceneDelegate{
                    if let view = sceneDelegate.window?.rootViewController?.view{
                      view.makeToast("Text")
                  }
              }
//...

Hope it helps.

GAlonso
  • 516
  • 5
  • 7
  • I couldn't solve it. I was trying to use some other approaches with alert, but not that good as a Toast. Your solution works fine, thanks for your help! – zampnrs Jul 03 '20 at 13:38
3

Try to use this open source: https://github.com/huynguyencong/ToastSwiftUI . I found that it is very easy to use.

struct ContentView: View {
    @State private var isShowingToast = false
    
    var body: some View {
        VStack(spacing: 20) {
            Button("Show toast") {
                self.isShowingToast = true
            }
            
            Spacer()
        }
        .padding()

        // Just add a modifier to show a toast, with binding variable to control
        .toast(isPresenting: $isShowingToast, dismissType: .after(3)) {
            ToastView(message: "Hello world!", icon: .info)
        }
    }
}
huynguyen
  • 7,616
  • 5
  • 35
  • 48
0

You can try this to show toast with Toast_Swift

if let view = UIApplication.shared.windows[0].rootViewController?.view {
    view.makeToast("text)
}
Shabnam Siddiqui
  • 579
  • 4
  • 13