有没有办法防止键盘消失?
我意识到这与大多数帖子相反,但我希望键盘保持向上状态即使按下“键盘向下”按钮。
具体来说,我有一个包含两个 UITextField 的视图。使用以下委托方法,
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return NO;
}
即使用户按下键盘上的“完成”按钮或点击屏幕上的任何其他位置(除了右下角那个讨厌的键盘向下按钮),我也可以保持键盘处于打开状态。键盘。
我像模态视图一样使用此视图(尽管该视图与推送到 UINavigationController 中的 ViewController 相关联),因此从用户角度来看,始终保持键盘处于打开状态确实效果最佳。如果有人知道如何实现这一目标,请告诉我!谢谢!
更新仍然没有解决方案!当按下Done
时,会触发textFieldShouldReturn
,但当按下Dismiss
按钮时,会触发textFieldDidEndEditing
。我无法阻止 textField
结束编辑,否则它永远消失。不知何故,我真的希望有一种方法可以检测 Dismiss
按钮并忽略它。如果您知道方法,请赐教!
I realize that this is the inverse of most posts, but I would like for the keyboard to remain up even if the 'keyboard down' button is pressed.
Specifically, I have a view with two UITextField
s. With the following delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return NO;
}
I am able to keep the keyboard up even if the user presses the Done
button on the keyboard or taps anywhere else on the screen EXCEPT for that pesky keyboard down button on the bottom right of the keyboard.
I am using this view like a modal view (though the view is associated with a ViewController that gets pushed in a UINavigationController), so it really works best from a user perspective to keep the keyboard up all of the time. If anyone knows how to achieve this, please let me know! Thanks!
UPDATE Still no solution! When Done
is pressed, it triggers textFieldShouldReturn
, but when the Dismiss
button is pressed, it triggers textFieldDidEndEditing
. I cannot block the textField
from ending editing or it never goes away. Somehow, I really want to have a method that detects the Dismiss
button and ignores it. If you know a way, please enlighten me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有一种方法可以做到这一点。因为
UIKeyboard
是UIWindow
的子类,所以唯一足够大的东西可以进入< code>UIKeyboard 的方式是另一种UIWindow
。这适用于 iPhone 应用程序。还没用 iPad 尝试过。您可能需要调整
myWindow
的大小。另外,我没有在myWindow
上进行任何内存管理。所以,也考虑这样做。There IS a way to do this. Because
UIKeyboard
subclassesUIWindow
, the only thing big enough to get inUIKeyboard
's way is anotherUIWindow
.This works on iPhone apps. Haven't tried it with iPad. You may need to adjust the size of
myWindow
. Also, I didn't do any mem management onmyWindow
. So, consider doing that, too.我想我已经找到了一个很好的解决方案。
添加一个 BOOL 作为实例变量,我们称之为
shouldBeginCalledBeforeHand
然后实现以下方法:
以及
防止键盘随返回按钮消失。诀窍是,焦点从一个文本字段切换到另一个文本字段将提前触发 textFieldShouldBeginEditing。如果按下关闭键盘按钮,则不会发生这种情况。文本字段获得焦点后,该标志会重置。
I think I've found a good solution.
Add a BOOL as instance variable, let's call it
shouldBeginCalledBeforeHand
Then implement the following methods:
As well as
to prevent the keyboard from disappearing with the return button. The trick is, a focus switch from one textfield to another will trigger a textFieldShouldBeginEditing beforehand. If the dismiss keyboard button is pressed this doesn't happen. The flag is reset after a textfield has gotten focus.
旧的不完美的解决方案
我只能想到一个不完美的解决方案。监听通知
UIKeyboardDidHideNotification
并再次将文本字段设置为第一响应者。这会将键盘移出视线并再次移回。您可以通过监听 UIKeyboardWillHideNotification 来记录哪个文本字段是最后一个firstResponder,并将焦点放在 didHide 中。Old not perfect solution
I can only think of a not perfect solution. Listen for the notification
UIKeyboardDidHideNotification
and make of the textfields first responder again. This will move the keyboard out of sight and back again. You could keep record of which textfield was the last firstResponder by listening for UIKeyboardWillHideNotification and put focus on it in the didHide.对于 iOS 9/10 和 Swift 3,使用它创建一个与“隐藏键盘”重叠的矩形 - 按钮
请注意,这会向键盘窗口而不是主窗口添加一个子视图
For iOS 9/10 and Swift 3, use this to create a rect which overlaps the "Hide keyboard" - Button
Notice that this adds a sub view to the keyboard window instead of the main window
尝试在键盘关闭按钮顶部添加自定义,以便用户无法点击关闭按钮。我在我的一个应用程序中使用了这种方法。
Try adding a custom on top of the keyboard dismiss button so that the user won't be able to tab the dismiss button. I have used this method in one of my application.
试试这个...
您可以使用 Nick Weaver 提到的通知。
Try this...
You can use notification as mentioned by Nick Weaver.