我正在制作一个具有 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];
}
并将其添加到您的 View 中并加载
[self addDoneToolBarToKeyboard:self.textView];
希望对你有帮助
关于ios - 在键盘顶部添加一个完成按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21635094/