ios - 在键盘顶部添加一个完成按钮

标签 ios iphone keyboard

我正在制作一个具有 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/

相关文章:

iOS 9 AVAssetDownloadURLSession 崩溃

iphone - UIView 动画结果不一致

linux - 在 Linux 上从 USB HID 键盘设备获取字符

ios - 当用户点击键盘时输出特定图像

ios - iOS 中的 HTTP "POST"请求

ios 9 通过 HDMI 闪电适配器播放 mp4 全屏视频

ios - swift 项目中的 ooVoo SDK 编译错误

iphone - 我想让我的应用程序仅在 ios 中横向显示(ios 5 和 ios 6)

iphone - 如何跟踪 UITableView 中所有选定单元格的索引

c++ - xInput 中的键盘和鼠标设备支持