Unfortunately, there isn't a way to add an action on tap outside of the MessageView
.
However, if your dimMode
is interactive
(has tap outside to dismiss enabled) you can easily catch .willHide
and .didHide
events using eventListeners
of SwiftMessages.Config
:
let view = MessageView.viewFromNib(layout: .cardView)
var config = SwiftMessages.defaultConfig
config.dimMode = .gray(interactive: false)
config.eventListeners.append { event in
switch event {
case .willHide:
// Handle will hide
case .didHide:
// Handle did hide
default:
break
}
}
SwiftMessages.show(config: config, view: view)
Those events will be triggered on tap outside of the MessageView
.
Update: In your particular case where you have a button which have a different action from the tap outside action, you can use something like this:
func showMessageView(buttonHandler: @escaping () -> Void, dismiss: @escaping () -> Void) {
var buttonTapped = false
let view = MessageView.viewFromNib(layout: .cardView)
view.buttonTapHandler = { sender in
buttonHandler()
buttonTapped = true
}
var config = SwiftMessages.defaultConfig
config.dimMode = .gray(interactive: false)
config.eventListeners.append { event in
if !buttonTapped, event == .didHide {
dismiss()
}
}
SwiftMessages.show(config: config, view: view)
}
That way when the button is tapped the dismiss
closure will never run.