【发布时间】:2014-03-05 07:09:47
【问题描述】:
我正在制作一个具有 UITextView 的通用应用程序。 当应用程序在 iPad 上运行时,右下角有一个按钮,可以让我关闭键盘。 iPhone版本没有这样的按钮。 我在一些 iPhone 应用程序上看到了键盘顶部的一个栏,它有一个完成选项。 是否有一种简单的方法可以将 iPad 风格的关闭键盘按钮也添加到 iPhone 应用程序中。 如果没有,将完成样式栏添加到键盘顶部的最佳方法是什么? 提前致谢。
【问题讨论】:
我正在制作一个具有 UITextView 的通用应用程序。 当应用程序在 iPad 上运行时,右下角有一个按钮,可以让我关闭键盘。 iPhone版本没有这样的按钮。 我在一些 iPhone 应用程序上看到了键盘顶部的一个栏,它有一个完成选项。 是否有一种简单的方法可以将 iPad 风格的关闭键盘按钮也添加到 iPhone 应用程序中。 如果没有,将完成样式栏添加到键盘顶部的最佳方法是什么? 提前致谢。
【问题讨论】:
请试试这个代码
//set up a placeholder variable for the textfield user typing
UITextView *currentTextView;
-(void)addDoneToolBarToKeyboard:(UITextView *)textView
{
UIToolbar* doneToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbar.barStyle = UIBarStyleBlackTranslucent;
doneToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonClickedDismissKeyboard)],
nil];
[doneToolbar sizeToFit];
textView.inputAccessoryView = doneToolbar;
}
//remember to set your text view delegate
//but if you only have 1 text view in your view controller
//you can simply change currentTextField to the name of your text view
//and ignore this textViewDidBeginEditing delegate method
- (void)textViewDidBeginEditing:(UITextView *)textView
{
currentTextView = textView;
}
-(void)doneButtonClickedDismissKeyboard
{
[currentTextView resignFirstResponder];
}
在你的视图中添加这个确实加载了
[self addDoneToolBarToKeyboard:self.textView];
希望有帮助
【讨论】:
doneWithNumberPad: 方法,在文本字段上实际辞职第一响应者。
[doneToolbar setTintColour:[UIColor whiteColor]];。
同样的答案swift3:
func addDoneToolBarToKeyboard(textView:UITextView)
{
let doneToolbar : UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))
doneToolbar.barStyle = UIBarStyle.default
let flexibelSpaceItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let hideKeyboardItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.dismissKeyboard))
doneToolbar.items = [flexibelSpaceItem, hideKeyboardItem!]
doneToolbar.sizeToFit()
textView.inputAccessoryView = doneToolbar
}
和解除功能将是:
func dismissKeyboard()
{
self.view.endEditing(true)
}
【讨论】:
您可以随时在一行代码中执行此操作:
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
这会将resignFirstResponder 发送到响应者链。当前文本视图将是第一响应者,因此它将收到消息并退出第一响应者。
【讨论】:
我知道这是一个为时已晚的答案,但在 iOS 8 中,我无法使用之前的答案隐藏我的 TEXT 视图的键盘。
仅供参考,我只是添加这个。
// Para esconder el teclado al oprimir el fondo de la pantalla
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.answerTextView endEditing:YES];
}
从theapplady提取
【讨论】: