MessageKit / MessageKit

A community-driven replacement for JSQMessagesViewController
https://messagekit.github.io
MIT License
6k stars 1.18k forks source link

MessageInputBar dismissed when an alert is presented #620

Closed clayellis closed 6 years ago

clayellis commented 6 years ago

General Information

@objc func handleTyping() {
    let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
    present(alert, animated: true, completion: nil)

    ...
}

What happened?

The MessageInputBar animates down offscreen when the alert is presented. It comes back after the alert is dismissed.

What did you expect to happen?

The alert to be presented and the MessageInputBar to remain visible.

My Question

This is happening because messageInputBar is the inputAccessoryView on MessagesViewController and is the correct behavior in UIKit.

My question is how do I prevent the MessageInputBar from disappearing when the alert is presented? I've tried overriding some of the first responder properties to prevent the input bar from being "dismissed" but nothing seems to be working quite yet. Is anyone aware of a solution?

clayellis commented 6 years ago

animated gif-source

Hmm, the embedded gif isn't working. https://giphy.com/gifs/2t9oMHhOAX7E75sNny

SD10 commented 6 years ago

@clayellis I'm not sure if this is possible. The MessageInputBar is the inputAccessoryView of the controller 🤔

clayellis commented 6 years ago

@SD10 right, which is why it isn't a "bug", it's supposed to work that way. I just don't know the fine details of what occurs in the first responder chain when a controller is presented from a controller who is currently the first responder. The closest I've come to achieving the behavior I'm looking for is calling becomeFirstResponder on the MessagesViewController in the presentation completion handler:

present(alert, animated: true, completion: { self.becomeFirstResponder() })

But things just get weird from that point. It isn't a good solution.

Other things I've tried:

clayellis commented 6 years ago

This SO question doesn't have any answers, but leaving a link to it here just in case at some point in the future someone answers or has the same question: https://stackoverflow.com/questions/46996251/inputaccessoryview-animating-down-when-alertcontroller-actionsheet-presented

cwalo commented 6 years ago

The trick is to present the alert from the topmost window and viewcontroller.

let topViewController = UIApplication.shared.windows.last!.rootViewController!
topViewController.present(alert, animated: true, completion: nil)

You could also try creating a new UIWindow above the Keyboard by giving it a higher WindowLevel and present the alert there. I suspect that's what Messages is probably doing since the previous is a bit clunky.

alert

clayellis commented 6 years ago

@cwalo That'd do the trick! I was going to use another window as a last resort, but it does seem to be a pretty clean solution! Thanks a ton, I really appreciate it.