Prevent keyboard from dismissing while changing vi

2019-09-21 22:53发布

问题:

On apps like say the stock messages app, if you’re in a conversation with the keyboard showing and swipe to go back to your conversation list, the keyboard remains up as the view gets swiped away.

I can’t seem to figure out how to mimic this behavior but I’ve seen it in other apps so it’s gotta be possible.

Edit: Not sure I understand why this is getting downvotes. It’s definitely a valid question where I couldn’t find the answer on google?

Edit 2: Here’s a video of what I’m trying to accomplish https://arxius.io/v/a555c8db compared to this behavior in discord https://arxius.io/v/0bfda09a

回答1:

On apps like say the stock messages app, if you’re in a conversation with the keyboard showing and swipe to go back to your conversation list, the keyboard remains up as the view gets swiped away.

I don't actually see that behavior, at least in the Messages app on an iPhone. That said, the keyboard should remain visible whenever the first responder can accept text. If you want to switch to a different view controller and keep the keyboard visible, then make sure that first responder in the new view graph is editable. For example, if there's a text field, you could set it to be the first responder.



回答2:

So before we talk about any kind of solution, let's learn exactly why the keyboard is dismissed.

As you know, iOS UI works on a view controller based system, with a text field ultimately being managed by a view controller somewhere.

When you click the back button, the active view controller is released from the navigation stack and therefore the system deduces that the text field is no longer in use, so it resigns it as the first responder.

Unfortunately there is no built in way to alter this behaviour and whilst we can speculate how apple might do it, we do not know.

The few options we have are not particularly neat or tidy, but a method I have had success with in the past is creating a hidden text field directly on the window before you pop the view controller and setting it first responder. Then on your other view controller, the text field can take first responder from the invisible one and remove it.

This is not ideal, but it's the only technique that comes to mind.



回答3:

It is correct that you had to have UITextFiled available for both views to have keyboard passed between them consistently. But nobody said that this textfield had to be on screen, or had to be part of ViewController's view. So all you had to do is to place this textFiled somewhere out of screen, for example as subview of your window, or NavigationController's view, make it first responder, and switch to your text field on view didAppear:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.textField.becomeFirstResponder()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    let fixedView = self.view.window
    let fakeTextField = fixedView?.subviews.last as? UITextField ?? UITextField()
    fakeTextField.autocapitalizationType = textField.autocapitalizationType
    fakeTextField.autocorrectionType = textField.autocorrectionType
    fakeTextField.center = CGPoint(x: -100, y: -100)
    fixedView?.addSubview(fakeTextField)
    fakeTextField.becomeFirstResponder()
}

Make sure that keyboard configuration is the same, otherwise you will have it switching.



标签: ios swift xcode