問題描述
我正在制作一個具有 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];
希望有幫助
這篇關于在鍵盤頂部添加完成按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!