在键盘顶部添加完成按钮

Add a done button to the top of the keyboard(在键盘顶部添加完成按钮)
本文介绍了在键盘顶部添加完成按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在制作一个具有 UITextView 的通用应用程序.当应用程序在 iPad 上运行时,右下角有一个按钮,可以让我关闭键盘.iPhone版本没有这样的按钮.我在一些 iPhone 应用程序上看到了键盘顶部的一个栏,它有一个完成选项.是否有一种简单的方法可以将 iPad 风格的关闭键盘按钮也添加到 iPhone 应用程序中.如果没有,将完成样式栏添加到键盘顶部的最佳方法是什么?提前致谢.

I am making a universal app that has a UITextView. When the app run on the iPad there is a button on the lower right which enables me to dismiss the keyboard. The iPhone version does not have such a button. I have seen on some iPhone apps a bar on top of the keyboard that has a done option. Is there an easy way to add an iPad style dismiss keyboard button to the iPhone app as well. If not, what is the best way to add a done style bar to the top of the keyboard? Thanks in advance.

推荐答案

请试试这个代码

 //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];
}

在你的视图中添加这个确实加载了

and add this in your view did load

 [self addDoneToolBarToKeyboard:self.textView];

希望有帮助

这篇关于在键盘顶部添加完成按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Pywin32 (com objects) on Mac(Mac 上的 Pywin32(com 对象))
convert big yml data(转换大 yml 数据)
Is there a native YAML library for iPhone?(是否有适用于 iPhone 的本机 YAML 库?)
View aligned to top of keyboard appears in wrong place in iOS 9 Shortcut Bar only mode(在 iOS 9 Shortcut Bar only 模式下,与键盘顶部对齐的视图出现在错误的位置)
Disable speech to text button (Micro phone) on soft input keyboard in android programmatically(以编程方式在android中的软输入键盘上禁用语音到文本按钮(麦克风))
UIkeyboard type(UI键盘类型)