你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS封閉虛擬鍵盤辦法匯總

iOS封閉虛擬鍵盤辦法匯總

編輯:IOS開發綜合

在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封閉虛擬鍵盤的辦法,年夜家可以依據小我須要選擇一種合適本身比擬好的辦法。同時也異常感激年夜家對本站網站的支撐!

【iOS封閉虛擬鍵盤辦法匯總】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved