問題描述
我正在制作一個(gè)具有 UITextView 的通用應(yīng)用程序.當(dāng)應(yīng)用程序在 iPad 上運(yùn)行時(shí),右下角有一個(gè)按鈕,可以讓我關(guān)閉鍵盤.iPhone版本沒有這樣的按鈕.我在一些 iPhone 應(yīng)用程序上看到了鍵盤頂部的一個(gè)欄,它有一個(gè)完成選項(xiàng).是否有一種簡(jiǎn)單的方法可以將 iPad 風(fēng)格的關(guān)閉鍵盤按鈕也添加到 iPhone 應(yīng)用程序中.如果沒有,將完成樣式欄添加到鍵盤頂部的最佳方法是什么?提前致謝.
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.
推薦答案
請(qǐng)?jiān)囋囘@個(gè)代碼
//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];
}
在你的視圖中添加這個(gè)確實(shí)加載了
and add this in your view did load
[self addDoneToolBarToKeyboard:self.textView];
希望有幫助
這篇關(guān)于在鍵盤頂部添加完成按鈕的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!