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



我正在尝试使用"Toast_Swift"显示自定义祝酒词。使用iOS方法channel。我已经在AppDelegate.swift中设置了方法通道,但是我不确定如何像库的使用示例中描述的那样显示toast。

AppDelegate.swift file -

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)

}
}

下面是Toast_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()

谁能帮助我如何使用Toastrongwift方法来显示祝酒词?

你已经创建了一个viewcontroller实例,所以请尝试这个,因为appdelegate没有任何UIView

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

而不是这一行

self.showCustomToast()

最新更新