Swift:在 iPhone 上启用 "dismiss keyboard" 按钮?
Swift: Enable "dismiss keyboard" button on iPhone?
我看到很多 post(例如 "hide keyboard for text field in swift programming language")关于如何在按下 return 按钮后或通过在 UITextView 外部单击(然后先退出)使键盘消失响应者,and/or 将 endEditing 设置为 true)。这不是我想做的;我既不想使用 return 键——我希望用户能够使用 return 键实际向他们在 UITextView 中输入的文本添加换行符——也不想强制用户单击外部以关闭键盘。
在 iPad 上有一个自然出现的 "dismiss keyboard" 按钮,作为键盘本身的一部分,可以关闭键盘。它通常在键盘的右下角(右shift键下方),是一个键盘的小图片,上面有一个向下的箭头。
如何在 iPhone 上启用此按钮,即让出现的键盘也包含关闭按钮?
这个post:"dismiss iphone UITextView Keyboard"好像提倡在键盘上方加一个工具栏。有没有一种方法可以简单地 select 一个包含关闭键选项的可选键盘,类似于在需要电子邮件地址时可以在主键盘屏幕上提供带有“@”的键盘的方式?
谢谢。
PS- 为清楚起见:我不想将 return 键设置为 "Done"(根据 this post),我想将其保留为return 键。如果这是唯一的方法,我愿意添加一个额外的 UIButton,但除了 return 键之外,似乎应该有一个键盘选项,其中已经包含一个关闭按钮。
我很遗憾地说,对您的请求的正确答案是没有任何可能的方法来做您想要的。
正如您从研究中了解到的那样,社区已经找到了很多解决方法,但我们只有这些。
添加通知,键盘即将显示时触发
然后自定义一个view,这里可以改成你的button
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPoint:) name:UIKeyboardWillShowNotification object:nil];
- (void) changeContentViewPoint:(NSNotification *)notification{
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGFloat keyBoardEndY = value.CGRectValue.origin.y; // get the y of the keyboard
NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// add animation, make your view move as the keyboard moves
[UIView animateWithDuration:duration.doubleValue animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:[curve intValue]];
_mainView.center = CGPointMake(_mainView.center.x, keyBoardEndY - STATUS_BAR_HEIGHT - _mainView.bounds.size.height/2.0); // keyBoardEndY including the height of the status bar, so get rid of this height.
}];
}
我看到很多 post(例如 "hide keyboard for text field in swift programming language")关于如何在按下 return 按钮后或通过在 UITextView 外部单击(然后先退出)使键盘消失响应者,and/or 将 endEditing 设置为 true)。这不是我想做的;我既不想使用 return 键——我希望用户能够使用 return 键实际向他们在 UITextView 中输入的文本添加换行符——也不想强制用户单击外部以关闭键盘。
在 iPad 上有一个自然出现的 "dismiss keyboard" 按钮,作为键盘本身的一部分,可以关闭键盘。它通常在键盘的右下角(右shift键下方),是一个键盘的小图片,上面有一个向下的箭头。
如何在 iPhone 上启用此按钮,即让出现的键盘也包含关闭按钮?
这个post:"dismiss iphone UITextView Keyboard"好像提倡在键盘上方加一个工具栏。有没有一种方法可以简单地 select 一个包含关闭键选项的可选键盘,类似于在需要电子邮件地址时可以在主键盘屏幕上提供带有“@”的键盘的方式?
谢谢。
PS- 为清楚起见:我不想将 return 键设置为 "Done"(根据 this post),我想将其保留为return 键。如果这是唯一的方法,我愿意添加一个额外的 UIButton,但除了 return 键之外,似乎应该有一个键盘选项,其中已经包含一个关闭按钮。
我很遗憾地说,对您的请求的正确答案是没有任何可能的方法来做您想要的。
正如您从研究中了解到的那样,社区已经找到了很多解决方法,但我们只有这些。
添加通知,键盘即将显示时触发
然后自定义一个view,这里可以改成你的button
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPoint:) name:UIKeyboardWillShowNotification object:nil];
- (void) changeContentViewPoint:(NSNotification *)notification{
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGFloat keyBoardEndY = value.CGRectValue.origin.y; // get the y of the keyboard
NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// add animation, make your view move as the keyboard moves
[UIView animateWithDuration:duration.doubleValue animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:[curve intValue]];
_mainView.center = CGPointMake(_mainView.center.x, keyBoardEndY - STATUS_BAR_HEIGHT - _mainView.bounds.size.height/2.0); // keyBoardEndY including the height of the status bar, so get rid of this height.
}];
}