iOS關(guān)閉虛擬鍵盤方法匯總
在iOS應(yīng)用開發(fā)中,有三類視圖對象會打開虛擬鍵盤,進(jìn)行輸入操作,但如何關(guān)閉虛擬鍵盤,卻沒有提供自動化的方法。這個(gè)需要我們自己去實(shí)現(xiàn)。這三類視圖對象分別是UITextField,UITextView和UISearchBar。 這里介紹一下UITextField中關(guān)閉虛擬鍵盤的幾種方法。
第一種方法,使用它的委托UITextFieldDelegate中的方法textFieldShouldReturn:來關(guān)閉虛擬鍵盤。
在UITextField視圖對象如birdNameInput所在的類中實(shí)現(xiàn)這個(gè)方法。
(BOOL)textFieldShouldReturn:(UITextField *)textField { if ((textField == self.birdNameInput) || (textField == self.locationInput)) { [textField resignFirstResponder]; } return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { if ((textField == self.birdNameInput) || (textField == self.locationInput)) { [textField resignFirstResponder]; } return YES; }
這樣,在輸入框birdNameInput中打開虛擬鍵盤后,輕擊鍵盤的return鍵就會自動關(guān)閉掉虛擬鍵盤。
第二種方法,將birdNameInput的屬性中Return Key修改為done,再定義一個(gè)方法和Done鍵的Did End On Exit連接。
通過輕擊done鍵觸發(fā)這個(gè)事件來關(guān)閉虛擬鍵盤。
定義的方法如下:
(IBAction) textFieldDoneEditing:(id)sender { [sender resignFirstResponder]; } - (IBAction) textFieldDoneEditing:(id)sender { [sender resignFirstResponder]; }
這兩個(gè)方法都是輕擊虛擬鍵盤上一個(gè)鍵來關(guān)閉它。這屬于精確操作,而手指不像鼠標(biāo),做這種操作不容易。因此就UI層面而言,這兩個(gè)方法都不是最好的方法。 在iphone或ipad屏幕上,虛擬鍵盤占用的面積大小是有限的。通過輕擊虛擬鍵盤之外的區(qū)域而關(guān)閉虛擬鍵盤。
第三種方法,通過輕擊鍵盤之外的空白區(qū)域關(guān)閉虛擬鍵盤。
在birdNameInput所屬的視圖控制器類的viewDidLoad方法中定義一個(gè)UITapGestureRecognizer的對象,然后將它賦值為它的視圖。
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; [tap release]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; [tap release];
再定義一下選擇器調(diào)用的方法dismissKeyboard。
(void)dismissKeyboard { [birdNameInput resignFirstResponder]; } -(void)dismissKeyboard { [birdNameInput resignFirstResponder]; }
如果屏幕上有多個(gè)textField的話,一個(gè)一個(gè)地列出來就有些麻煩。那么將方法修改一下,如下:
(void)dismissKeyboard { NSArray *subviews = [self.view subviews]; for (id objInput in subviews) { if ([objInput isKindOfClass:[UITextField class]]) { UITextField *theTextField = objInput; if ([objInput isFirstResponder]) { [theTextField resignFirstResponder]; } } } } -(void)dismissKeyboard { NSArray *subviews = [self.view subviews]; for (id objInput in subviews) { if ([objInput isKindOfClass:[UITextField class]]) { UITextField *theTextField = objInput; if ([objInput isFirstResponder]) { [theTextField resignFirstResponder]; } } } }
如果這個(gè)屏幕上的視圖對象很復(fù)雜的話,另當(dāng)別論。 這個(gè)方法是編碼新建一個(gè)手勢對象。也可以直接使用interface builder圖形化開發(fā)工具,在storyboard中拉入一個(gè)手勢對象到視圖控制器類中,再將此手勢對象建立一個(gè)IBACTION,名稱可以是dismissKeyboard。
第四種方法,通過輕擊鍵盤之外的空白區(qū)域關(guān)閉虛擬鍵盤。
將屏幕上的view也就是textField的父視圖拖一個(gè)touch down事件出來,和一個(gè)能關(guān)閉虛擬鍵盤的方法連接。如果視圖沒有touch down事件,可將view的父類從UIView修改為UIButton。 首先定義并實(shí)現(xiàn)一個(gè)方法backgroundTap:。
(IBAction) backgroundTap:(id)sender { NSArray *subviews = [self.view subviews]; for (id objInput in subviews) { if ([objInput isKindOfClass:[UITextField class]]) { UITextField *theTextField = objInput; if ([objInput isFirstResponder]) { [theTextField resignFirstResponder]; } } } } - (IBAction) backgroundTap:(id)sender { NSArray *subviews = [self.view subviews]; for (id objInput in subviews) { if ([objInput isKindOfClass:[UITextField class]]) { UITextField *theTextField = objInput; if ([objInput isFirstResponder]) { [theTextField resignFirstResponder]; } } } }
然后選擇背景視圖的Touch Down事件,連接 backgroundTap:即可。這樣只要輕擊一下虛擬鍵盤之外的區(qū)域,就能關(guān)閉虛擬鍵盤。這些方法都是使用resignFirstResponder方法來關(guān)閉虛擬鍵盤,還有其他的方法。
第五種方法,使用endEditing:方法 在所在的視圖控制器類中,覆蓋這個(gè)方法。
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:YES]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:YES]; }
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
但是,如果這個(gè)屏幕很復(fù)雜,虛擬鍵盤之外的區(qū)域中有很多按鈕。輕擊這些區(qū)域時(shí)可能會輕擊到這些按鈕,這樣虛擬鍵盤就不能關(guān)閉。
要是找到一個(gè)沒有按鈕的空白區(qū)域都不容易且還有隱藏的視圖對象時(shí),通過輕擊虛擬鍵盤之外的區(qū)域關(guān)閉虛擬鍵盤的方法實(shí)現(xiàn)起來就難了。
第六種方法,覆蓋hitTest:withEvent:方法關(guān)閉虛擬鍵盤
在stackoverflow.com上,有人這樣總結(jié)。說使用hitTest:withEvent:方法是最好的,也是最容易的解決方法。
I think the easiest (and best) way to do this is to subclass your global view and use hitTest:withEvent method to listen to any touch. Touches on keyboard aren't registered, so hitTest:withEvent is only called when you touch/scroll/swipe/pinch... somewhere else, then call [self endEditing:YES]. This is better than using touchesBegan because touchesBegan are not called if you click on a button on top of the view. It is better than UITapGestureRecognizer which can't recognize a scrolling gesture for example. It is also better than using a dim screen because in a complexe and dynamic user interface, you can't put dim screen every where. Moreover, it doesn't block other actions, you don't need to tap twice to select a button outside (like in the case of a UIPopover). Also, it's better than calling [textField resignFirstResponder], because you may have many text fields on screen, so this works for all of them.
因此,我再建立一個(gè)繼承UIView的視圖類。在這個(gè)視圖類中,覆蓋hitTest:withEvent:方法,增加[self endEditing:YES]方法。
(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *result = [super hitTest:point withEvent:event]; [self endEditing:YES] return result; } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *result = [super hitTest:point withEvent:event]; [self endEditing:YES] return result; }
我將視圖控制器的主視圖所屬類修改為這個(gè)新建視圖類。這樣在屏幕上輕擊任何位置都會關(guān)閉虛擬鍵盤。 這個(gè)方法是最簡單,也是最好的關(guān)閉虛擬鍵盤的方法。 使用好hitTest:withEvent:這個(gè)方法,還可以實(shí)現(xiàn)很多很復(fù)雜的功能。
The implementation of hitTest:withEvent: in UIResponder does the following:
• It calls pointInside:withEvent: of self
• If the return is NO, hitTest:withEvent: returns nil. the end of the story.
• If the return is YES, it sends hitTest:withEvent: messages to its subviews. it starts from the top-level subview, and continues to other views until a subview returns a non-nil object, or all subviews receive the message.
• If a subview returns a non-nil object in the first time, the first hitTest:withEvent: returns that object. the end of the story.
• If no subview returns a non-nil object, the first hitTest:withEvent: returns self
This process repeats recursively, so normally the leaf view of the view hierarchy is returned eventually. However, you might override hitTest:withEvent to do something differently. In many cases, overriding pointInside:withEvent: is simpler and still provides enough options to tweak event handling in your application.
以上給大家介紹了六種iOS關(guān)閉虛擬鍵盤的方法,大家可以根據(jù)個(gè)人需要選擇一種適合自己比較好的方法。同時(shí)也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
IOS 開發(fā)狀態(tài)欄隱藏的實(shí)現(xiàn)辦法
這篇文章主要介紹了IOS 開發(fā)狀態(tài)欄隱藏的實(shí)現(xiàn)辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02UIScrollView實(shí)現(xiàn)六棱柱圖片瀏覽效果
這篇文章主要為大家介紹了UIScrollView實(shí)現(xiàn)六棱柱圖片瀏覽效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07iOS開發(fā)中runtime常用的幾種方法示例總結(jié)
Runtime也就是所謂的“運(yùn)行時(shí)”,因?yàn)槭窃谶\(yùn)行時(shí)實(shí)現(xiàn)的。下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)中runtime常用的幾種方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-08-08IOS開發(fā)之由身份證號碼提取性別的實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS開發(fā)之由身份證號碼提取性別的實(shí)現(xiàn)代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07IOS 開發(fā)之UITableView 刪除表格單元寫法
這篇文章主要介紹了IOS 開發(fā)之UITableView 刪除表格單元寫法的相關(guān)資料,這里提供實(shí)例幫助大家實(shí)現(xiàn)該功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08Swift中的HTTP請求體Request Bodies使用示例詳解
這篇文章主要為大家介紹了Swift中的HTTP請求體Request Bodies使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02