swift Flutter -使用iOS平台方法显示来自第三方库的自定义吐司

bis0qfac  于 2023-04-10  发布在  Swift
关注(0)|答案(1)|浏览(129)

我尝试使用“Toast_Swift”库和iOS方法通道显示自定义吐司。我已经在AppDelegate.swift中设置了方法通道,但我不确定如何显示toast,如库的使用示例中所述。
AppDelegate.swift文件-

import UIKit
import Flutter
import Toast_Swift

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
   
      
      
      let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
          let channel = FlutterMethodChannel(name:"samples.flutte.    .dev/toast",binaryMessenger:controller.binaryMessenger)
      channel.setMethodCallHandler({
            (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
            
          guard call.method == "getToast" else {
              result(FlutterMethodNotImplemented)
              return
            }
          self.showCustomToast()
          
          })
      
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    
    
    private func showCustomToast() {
         let myView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 
          80.0, height: 400.0))

        //something wrong is going on here related to self.view
        self.view.makeToast("This is a piece of toast")
        self.view.showToast(myView)
       
    }
}

下面是吐司_Swift库https://github.com/scalessec/Toast-Swift的使用示例

// basic usage
self.view.makeToast("This is a piece of toast")

// toast with a specific duration and position
self.view.makeToast("This is a piece of toast", duration: 3.0, position: .top)

// toast presented with multiple options and with a completion closure
self.view.makeToast("This is a piece of toast", duration: 2.0, point: CGPoint(x: 110.0, y: 110.0), title: "Toast Title", image: UIImage(named: "toast.png")) { didTap in
    if didTap {
        print("completion from tap")
    } else {
        print("completion without tap")
    }
}

// display toast with an activity spinner
self.view.makeToastActivity(.center)

// display any view as toast
self.view.showToast(myView)

// immediately hides all toast views in self.view
self.view.hideAllToasts()

有谁能告诉我如何使用吐司_swift方法来显示一个toast?

wnavrhmk

wnavrhmk1#

您已经创建了一个viewcontroller示例,因此请尝试此操作,因为appdelegate没有任何UIView

controller.view.makeToast("This is a piece of toast")

而不是这一行

self.showCustomToast()

相关问题