在键盘顶部添加完成按钮 [英] Add a done button to the top of the keyboard

查看:14
本文介绍了在键盘顶部添加完成按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

希望有帮助

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆