2011-04-19 101 views
38

我希望能夠在用戶輕敲鍵盤外的任何地方時關閉iPhone鍵盤。我怎麼能這樣做呢?我知道我需要解僱響應者,但是當用戶敲出鍵盤空間時需要知道如何實現它。如果用戶點擊屏幕鍵盤,我該如何解除鍵盤鎖定?

+0

你可能會覺得這很有用:https://github.com/michaeltyson/TPKeyboardAvoiding – 2011-04-19 04:01:48

回答

73

你需要添加一個UITapGestureRecogniser,並將其分配給視圖,然後調用辭職在它選擇的文本字段第一個響應。

的代碼:

viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                     action:@selector(dismissKeyboard)]; 

[self.view addGestureRecognizer:tap]; 

dismissKeyboard:

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

(其中aTextField是負責用於鍵盤的文本字段)

OPTION 2

如果你不能添加gestureRecognizer那麼你可以試試這個

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch * touch = [touches anyObject]; 
    if(touch.phase == UITouchPhaseBegan) { 
     [aTextField resignFirstResponder]; 
    } 
} 
+0

非常優雅的解決方案w /手勢識別器! – 2011-04-20 16:45:56

+4

這很棒,但是,手勢操作正在吃掉視圖上的所有按鈕點擊事件。有任何想法嗎? – kmehta 2011-04-29 21:09:59

+0

解決鍵盤在ios中的好解決方案。 – 2013-01-31 10:57:16

1

您需要在鍵盤下面添加一個透明的UIVIew作爲子視圖,並在那裏處理觸摸以關閉鍵盤。以下代碼供您參考。

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(overlayTouched:)]; 
gesture.delegate = self; 
[(UITapGestureRecognizer *)gesture setNumberOfTouchesRequired:1]; 

UIView* trans = [[UIView alloc] initWithFrame:[[delegate view] bounds]]; 
[trans setOpaque:NO]; 
[trans setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin]; 
[trans setAlpha:0.3]; 
[trans setUserInteractionEnabled:YES]; 
trans.multipleTouchEnabled = YES; 
[trans addGestureRecognizer:gesture]; 
[trans setBackgroundColor:[UIColor blackColor]]; 
[trans setTag:BLACK_SCREEN_VIEW]; 
39

我用最簡單的辦法是這樣的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

     [[self view] endEditing:TRUE]; 

} 

的endEditing命令可用於上包含您的文本字段作爲子視圖的任何視圖。這種方法的另一個優點是你不需要知道哪個文本字段觸發了鍵盤。因此,即使您有多個文本框,只需將此行添加到超級視圖即可。

基於Apple的文檔,我認爲這種方法專門用於解決這個問題。

+0

「UIScrollView」呢? – testing 2014-12-01 13:47:50

+0

這是最好的解決方案沒有DOUBT! – StackUnderflow 2017-03-31 12:01:11

15

添加tapGesture識別器,但要確保cancelsTouchesInView = NO

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTextInput)]; 
tapGesture.cancelsTouchesInView = NO; 
[self.view addGestureRecognizer:tapGesture]; 
[tapGesture release]; 
+0

關於tapGesture.cancelsTouchesInView = NO; 非常感謝! – Miros 2014-03-17 14:48:22

0

做其他的方法很簡單:

讓您UIView as UIControl in custom class in the interface builder,那麼您可以在您的UIView : UIControlTouch up inside event附加一個IBAction方法,然後你把[yourTextField resignFirstResponder]放在IBAction method裏面,像這樣:

- (IBAction) hideKeyboard: (id) sender 
{ 
    // If you have more than one textfield to dismiss, of course only can be active 1, but here you can't know who is it, because sender will be the UIView : UIControl 
    [alias resignFirstResponder]; 
    [password resignFirstResponder]; 
} 

然後,您還有其他選擇,它將在界面構建器中放入您的文本字段the return key of the keyboard as Done(它可以是您希望的任何一種,但完成它對此很有用,因爲返回意味着對錶單執行操作),所以您可以按Done並隱藏鍵盤,但在這種情況下,您必須將以前的IBAction方法附加到Did end on exit事件中。

這樣鍵盤就會隱藏touching outside或觸摸鍵盤上的Done

如果你想改善的代碼,如果只將隱藏鍵盤從鍵盤觸摸Done的方法應該是:

// Attach all textFields here on Did end on exit event, will not work if touch outside the keyboard 
- (IBAction) hideKeyboard: (id) sender 
{ 
     [sender resignFirstResponder]; 
} 
0

如果你是一個UITableViewController或有UITableView,該用戶將被與你的ViewDidLoad交互,你可以簡單地做:

tableView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag; 

注意:這是Xamarin.iOS語法。

2

您需要添加一個UITapGestureRecogniser並將其分配給該視圖,然後在其選擇器上的文本字段上調用resigned first responder。

的代碼:

在viewDidLoad中

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                     action:@selector(dismissKeyboard)]; 

[self.view addGestureRecognizer:tap]; 

在dismissKeyboard:

-(void)dismissKeyboard { 
     [self.view endEditing:true]; 
} 
0

這是一個夫特4溶液:

let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard)) 

self.view.addGestureRecognizer(tap) 

而dismissKeyboard

@objc func dismissKeyboard() { 
    self.view.endEditing(true) 
}