首页 > 解决方案 > 如何使用 Swift 在我的应用程序中使用 SwiftMessage cocoapod?

问题描述

在我的场景中,我正在尝试将卡片弹出顶部(如吐司消息)集成到我的应用程序中。因此,我决定将SwiftMeesagecocoa pod 集成到我的应用程序中。我没有得到适当的逐步集成程序。下面的步骤我已经完成,但我需要进一步澄清。

https://github.com/SwiftKickMobile/SwiftMessages

  1. 已安装pod 'SwiftMessages
  2. 已创建CardView.xib
  3. CardView.xib 类和模块 我需要提供什么?

如何从多个视图控制器调用此 xib。我正在启用按钮,所以我需要处理按钮操作。根据存储库文档信息类和模块信息未正确提供。请提供集成此模块的步骤。

自定义类 UIView

guard let view = Bundle.main.loadNibNamed("ToastView.", owner: nil, options: nil)?.first as? ToastView else { return }
var config = SwiftMessages.defaultConfig
config.presentationContext = .window(windowLevel: UIWindow.Level.statusBar)
config.duration = SwiftMessages.Duration.seconds(seconds: 5) // show in 5 seconds for example
SwiftMessages.show(config: config, view: view)

标签: iosswift

解决方案


我建议您使用自定义视图而不是默认视图,CardView然后您可以进行更多自定义。

代码可能是这样的:

guard let view = Bundle.main.loadNibNamed("YourCustomView.", owner: nil, options: nil)?.first as? YourCustomView.  else { return }
var config = SwiftMessages.defaultConfig
config.presentationContext = .window(windowLevel: UIWindow.Level.statusBar)
config.duration = SwiftMessages.Duration.seconds(seconds: 5) // show in 5 seconds for example 
SwiftMessages.show(config: config, view: view)

如果您不想使用自定义视图

let view = MessageView.viewFromNib(layout: .cardView)
// Customize icon, title, body... here
SwiftMessages.show(config: config, view: view)

推荐阅读