如何让键盘消失? [英] How can I make the keyboard disappear?
问题描述
有没有办法使键盘消失,而不用 resignFirstResponder
?我有一个 UITextField
,我希望它能够接受新的文本,仍然有插入点(闪烁光标)可见,没有键盘在屏幕上。
Is there a way to make a keyboard disappear without resignFirstResponder
? I have a UITextField
, and I'd like it to be able to accept new text and still have the insertion point (flashing cursor) visible without the keyboard being on screen.
当我执行 [textField resignFirstResponder]
时,我仍然可以通过添加到 textField.text
,但我再也看不到插入点。
When I perform a [textField resignFirstResponder]
I can still insert text, by appending to the textField.text
, but I can't see the insertion point anymore.
有一个标准的方法让键盘动画,而我的textField仍然是第一响应者?
Is there a standard way to make the keyboard animate out, while having my textField remain first responder?
谢谢。
推荐答案
找到这个问题的答案。这不是标准,但是,像Dave说,可能是禁忌的应用程序审阅者。使用此页的代码作为起点:
Found an answer to this question. It's not standard though, and like Dave says, may be taboo for the app reviewers. Using the code from this page as a starting point:
http://unsolicitedfeedback.com/2009/02/06/how-to-customize-uikeyboard-by-adding-subviews/
我在动画块中添加。您可以使用标准动画来关闭键盘视图,但第一个响应者将保留该状态。真的,键盘只是隐藏屏幕。
I added in an animation block. You can dismiss the keyboards view with a standard looking animation, but whatever is first responder will retain that status. Really, the keyboard is just hidden off screen.
- (void)hideKeyboard
{
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
// Now iterating over each subview of the available windows
for (UIView *keyboard in [keyboardWindow subviews]) {
// Check to see if the view we have referenced is UIKeyboard.
// If so then we found the keyboard view that we were looking for.
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
// animate the keyboard moving
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.4];
// remove the keyboard
CGRect frame = keyboard.frame;
// if (keyboard.frame.origin.x >= 0) {
if (keyboard.frame.origin.y < 480) {
// slide the keyboard onscreen
//frame.origin.x = (keyboard.frame.origin.x - 320);
// slide the keyboard onscreen
frame.origin.y = (keyboard.frame.origin.y + 264);
keyboard.frame = frame;
}
else {
// slide the keyboard off to the side
//frame.origin.x = (keyboard.frame.origin.x + 320);
// slide the keyboard off
frame.origin.y = (keyboard.frame.origin.y - 264);
keyboard.frame = frame;
}
[UIView commitAnimations];
}
}
}
}
在代码中写出来将键盘关闭到屏幕的左侧和底部。我不知道当你最终 resignFirstResponder
时会发生什么,但是当你这样做时可能会重置键盘框架。
I wrote in code to dismiss the keyboard to the left and to the bottom of the screen. I don't know what will happen when you eventually resignFirstResponder
, but it might be an idea to reset the keyboard frame when you do that.
这篇关于如何让键盘消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!