【问题标题】:How to use Toast-Swift in SwiftUI?如何在 SwiftUI 中使用 Toast-Swift?
【发布时间】:2020-06-01 01:06:49
【问题描述】:

我想在 SwiftUI 视图中通过 CocoaPods 使用这个项目 Toast-Swift。是针对 UIView 的,所以我试着写了一个 ViewController 并把它包装到 SwiftUI 中,结果屏幕上什么都没有。

我的代码:

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)
    }
}

我在 Toast for SwiftUI (SO question) 的 SO 上找到了一些自定义实现,但它们的行为并不完全符合我的要求。

有人可以帮我解决这个问题吗? SwiftUI 中的 Toast 还有其他推荐吗?提前致谢!

【问题讨论】:

标签: ios uiviewcontroller swiftui


【解决方案1】:

我把它放在这里是为了那些仍在使用 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!")
        }
    }
}

【讨论】:

    【解决方案2】:

    我不知道您是否已经解决了您的问题,但我将其发布在这里以防有人感兴趣。我设法做到这一点,获得对 SceneManager 的引用,然后使用它的 rootViewController 视图。使用此视图,您可以使用 Toast_Swift 示例:

    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")
                      }
                  }
    //...
    

    希望对你有帮助。

    【讨论】:

    • 我无法解决。我试图在警报中使用其他一些方法,但不如 Toast 好。您的解决方案运行良好,感谢您的帮助!
    【解决方案3】:

    尝试使用这个开源:https://github.com/huynguyencong/ToastSwiftUI。我发现它非常好用。

    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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 2012-06-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多