iOS關閉虛擬鍵盤方法匯總_IOS

來源:互聯網
上載者:User

在iOS應用開發中,有三類別檢視對象會開啟虛擬鍵盤,進行輸入操作,但如何關閉虛擬鍵盤,卻沒有提供自動化的方法。這個需要我們自己去實現。這三類別檢視對象分別是UITextField,UITextView和UISearchBar。 這裡介紹一下UITextField中關閉虛擬鍵盤的幾種方法。

第一種方法,使用它的委託UITextFieldDelegate中的方法textFieldShouldReturn:來關閉虛擬鍵盤。

在UITextField視圖對象如birdNameInput所在的類中實現這個方法。

(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鍵就會自動關閉掉虛擬鍵盤。

第二種方法,將birdNameInput的屬性中Return Key修改為done,再定義一個方法和Done鍵的Did End On Exit串連。

通過輕擊done鍵觸發這個事件來關閉虛擬鍵盤。

定義的方法如下:

(IBAction) textFieldDoneEditing:(id)sender { [sender resignFirstResponder]; } - (IBAction) textFieldDoneEditing:(id)sender{[sender resignFirstResponder];}

這兩個方法都是輕擊虛擬鍵盤上一個鍵來關閉它。這屬於精確操作,而手指不像滑鼠,做這種操作不容易。因此就UI層面而言,這兩個方法都不是最好的方法。 在iphone或ipad螢幕上,虛擬鍵盤佔用的面積大小是有限的。通過輕擊虛擬鍵盤之外的地區而關閉虛擬鍵盤。

第三種方法,通過輕擊鍵盤之外的空白地區關閉虛擬鍵盤。

在birdNameInput所屬的視圖控制器類的viewDidLoad方法中定義一個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];

再定義一下選取器調用的方法dismissKeyboard。

(void)dismissKeyboard { [birdNameInput resignFirstResponder]; } -(void)dismissKeyboard {[birdNameInput resignFirstResponder];}

如果螢幕上有多個textField的話,一個一個地列出來就有些麻煩。那麼將方法修改一下,如下:

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

如果這個螢幕上的視圖對象很複雜的話,另當別論。 這個方法是編碼建立一個手勢對象。也可以直接使用interface builder圖形化開發工具,在storyboard中拉入一個手勢對象到視圖控制器類中,再將此手勢對象建立一個IBACTION,名稱可以是dismissKeyboard。

第四種方法,通過輕擊鍵盤之外的空白地區關閉虛擬鍵盤。

將螢幕上的view也就是textField的父視圖拖一個touch down事件出來,和一個能關閉虛擬鍵盤的方法串連。如果視圖沒有touch down事件,可將view的父類從UIView修改為UIButton。 首先定義並實現一個方法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:即可。這樣只要輕擊一下虛擬鍵盤之外的地區,就能關閉虛擬鍵盤。這些方法都是使用resignFirstResponder方法來關閉虛擬鍵盤,還有其他的方法。

第五種方法,使用endEditing:方法 在所在的視圖控制器類中,覆蓋這個方法

(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.

但是,如果這個螢幕很複雜,虛擬鍵盤之外的地區中有很多按鈕。輕擊這些地區時可能會輕擊到這些按鈕,這樣虛擬鍵盤就不能關閉。

要是找到一個沒有按鈕的空白地區都不容易且還有隱藏的視圖對象時,通過輕擊虛擬鍵盤之外的地區關閉虛擬鍵盤的方法實現起來就難了。

第六種方法,覆蓋hitTest:withEvent:方法關閉虛擬鍵盤

在stackoverflow.com上,有人這樣總結。說使用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.

因此,我再建立一個繼承UIView的視圖類。在這個視圖類中,覆蓋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;}

我將視圖控制器的主視圖所屬類修改為這個建立視圖類。這樣在螢幕上輕擊任何位置都會關閉虛擬鍵盤。 這個方法是最簡單,也是最好的關閉虛擬鍵盤的方法。 使用好hitTest:withEvent:這個方法,還可以實現很多很複雜的功能。

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關閉虛擬鍵盤的方法,大家可以根據個人需要選擇一種適合自己比較好的方法。同時也非常感謝大家對雲棲社區網站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.