防止键盘在更改视图时关闭 [英] Prevent keyboard from dismissing while changing views

查看:24
本文介绍了防止键盘在更改视图时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在诸如股票消息应用之类的应用中,如果您在对话时显示键盘并滑动以返回对话列表,则当视图被滑动时键盘会保持打开状态.

我似乎不知道如何模仿这种行为,但我在其他应用中看到过,所以它一定是可能的.

不确定我是否理解为什么这会被否决.我在谷歌上找不到答案,这绝对是一个有效的问题?

编辑 2:这是我正在尝试完成的视频

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

解决方案

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.

这篇关于防止键盘在更改视图时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆