我想知道最簡單的代碼,以便在數字鍵盤外部的任意位置點擊時關閉數字鍵盤鍵盤。這是一個簡單的應用程序,用於在文本字段中輸入數字,然後用戶可以在用戶輸入文本字段上的數字後關閉鍵盤。謝謝! :)如何通過點擊任何地方來關閉數字鍵盤鍵盤
回答
通常情況下,當用戶觸摸「取消」按鈕,我用它來關閉鍵盤 - 如果你要當用戶在鍵盤區域外的任何位置觸摸來做到這一點
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
NSLog(@"cancel clicked");
[searchBar resignFirstResponder]; // dismiss keyboard
return;
}
,然後您需要實現touchesEnded
&把這段代碼有 -
/* Delegate for when touch ends. */
-(void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
[searchBar resignFirstResponder];
}
我自己還沒有嘗試過,但我偵察這應該做的伎倆......
我不得不爲我的UITableView實現一個類似的UX(其中出現了UITextField)。首先,將一個UITapGestureRecognizer
添加到tableView。這將開始抓住從現在開始的所有水龍頭。
UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.tableView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
的副作用然而,你不希望當用戶點擊上的UITextField本身駁回鍵盤。所以,這種情況必須加以區分。
的handleTap方法的實現看起來是喜歡 -
/*
In the text editing mode, listens to all taps occurring on the table view to exit the mode. There are two special cases to consider:
a) Text field's clear button
b) Text field
*/
-(void)handleTap:(UITapGestureRecognizer*)tapRecognizer
{
if(tapRecognizer.state == UIGestureRecognizerStateEnded)
{
//Figure out where the user tapped
CGPoint p = [tapRecognizer locationInView:textField];
CGRect textFieldBounds = textField.bounds;
CGRect clearButtonBounds = CGRectMake(textFieldBounds.origin.x + textFieldBounds.size.width - 44, textFieldBounds.origin.y, 44, textFieldBounds.size.height);
if(CGRectContainsPoint(clearButtonBounds, p))
textField.text = @"";
if(CGRectContainsPoint(textFieldBounds, p))
return;
[textField resignFirstResponder];
//remove the tap gesture recognizer that was added.
for(id element in self.tableView.gestureRecognizers)
{
if([element isKindOfClass:[UITapGestureRecognizer class]])
{
[self.tableView removeGestureRecognizer:element];
}
}
}
[self commitNewText];
}
}
HTH。如果您喜歡,請將其標記爲答案。
-Akshay
觸摸背景關閉鍵盤
去,如果你是不是已經有Xcode的。我們需要爲控制器類增加一個動作。將以下行添加到Control_FunViewController.h文件中:
#import <UIKit/UIKit.h>
@interface Control_FunViewController : UIViewController {
UITextField *nameField;
UITextField *numberField;
}
@property (nonatomic, retain) IBOutlet UITextField *nameField; ]
@property (nonatomic, retain) IBOutlet UITextField *numberField;
- (IBAction)textFieldDoneEditing:(id)sender;
- (IBAction)backgroundTap:(id)sender;
@end
保存頭文件;切換到實現文件,並添加此代碼,該代碼只是簡單地告訴兩個文本字段,如果它們具有第一個響應者狀態。在不是第一響應者的控件上調用resignFirstResponder是完全安全的,因此我們可以安全地在兩個文本字段上調用它,而不必檢查是否是第一響應者。
- (IBAction)backgroundTap:(id)sender {
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
提示 保存此文件,並回到界面生成器。我們現在需要更改我們的筆尖視圖的基礎類。如果您查看筆尖的主窗口,您會看到該視圖中有三個圖標。第三個叫View的是我們的筆尖的主視圖,它把所有其他的控件和視圖看作子視圖。 單擊名稱爲View的圖標,它代表我們的筆尖的容器視圖。按␣4調出身份檢查員。這是我們可以在Interface Builder中更改任何對象實例的基礎類的地方。
在編寫代碼時,您將在頭文件和實現文件之間切換。幸運的是,Xcode有一個組合鍵,可以快速切換這些文件。默認組合鍵是␣␣␣(選項 - 向上箭頭),儘管您可以使用Xcode的首選項將其更改爲任何您想要的內容。76
標記類字段目前說的UIView。將其更改爲讀取UIControl。所有能夠觸發動作方法的控件都是UIControl的子類,所以通過改變底層類,我們只是讓這個視圖能夠觸發動作方法。您可以通過按␣2來調出連接檢查器來驗證。
將Touch Down事件拖到File's Owner圖標,然後選擇backgroundTap:操作。現在,在沒有主動控制的情況下觸摸視圖中的任何位置都會觸發我們的新操作方法,這會導致鍵盤縮回。
注意 保存筆尖,讓我們回去試試吧。再次編譯並運行您的應用程序。這次,鍵盤應該不僅在完成按鈕被點擊時消失,而且當您點擊任何不是活動控件的地方時,這也是您的用戶期望的行爲。
感謝您的先生rptwsthi! :)欣賞你的文章。 – jsanmtosj
只是做一個簡單的功能,像這樣的..
-(IBAction)hideKeyboard:(id)Sender
{
[_textValue resignFirstResponder];
}
現在去UR筆尖文件,並與didEndOnExit
文本字段連接此功能,你是好去。現在,當你點擊你的文本框時,鍵盤會隱藏自己。
試試這個方法,
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
現在任何地方點擊並查看鍵盤將解僱。 :-)
如果你已經有了一個自定義的數字鍵盤或其他酥料餅,另一種解決方案是使用手勢識別在你的UIView這樣初始化:
tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapper.cancelsTouchesInView = NO;
[self addGestureRecognizer:tapper];
_yourViewController = [[CustomViewController alloc] init];
_yourPopoverController = [[UIPopoverController alloc] initWithContentViewController:_yourViewController];
_yourPopoverController.popoverContentSize = CGSizeMake(550, 300);
_yourViewController.delegate = self;
而且有handleSingleTap解僱該酥料餅如果可見光和解僱編輯:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
if ([_yourPopoverController isPopoverVisible]) {
[_yourPopoverController dismissPopoverAnimated:YES];
}
[self endEditing:YES];
}
在Xcode 5使用點擊手勢識別器,並用行動.H宣佈選擇了一個名字和.M
- (IBAction)backgroundTap:(id)sender {
[self.view endEditing: YES];
}
我試圖實施一些解決方案在這裏,發現有一些問題與他們。值得注意的是,我的UIView
包含UIScrollView
,這似乎攔截觸摸。
如果失敗了,我認爲「隨處輕敲」有點不明確,需要用戶猜測如何關閉鍵盤而不是給他們明確的指導。
此外,我在應用程序中顯示的每個其他鍵盤上都有一個「返回」或「完成」按鈕,因此我決定爲用戶提供一種簡單,直觀,明確指定的方式來解除鍵盤上的數字鍵盤與其他正在使用的鍵盤解除機制一致。
我知道這不是什麼OP特別要求,但我相信這是比「點擊屏幕任意」要求更好的解決方案(這很容易實現!)。
下面的代碼被稱爲在我UIViewController
的-viewDidLoad
一部分。
// My app is restricted to portrait-only, so the following works
UIToolbar *numberPadAccessoryInputView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)];
// My app-wide tint color is a gold-ish color, so darkGray contrasts nicely
numberPadAccessoryInputView.barTintColor = [UIColor darkGrayColor];
// A basic "Done" button, that calls [self.textField resignFirstResponder]
UIBarButtonItem *numberPadDoneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.textField action:@selector(resignFirstResponder)];
// It's the only item in the UIToolbar's items array
numberPadAccessoryInputView.items = @[numberPadDoneButton];
// In case the background of the view is similar to [UIColor darkGrayColor], this
// is put as a contrasting edge line at the top of the UIToolbar
UIView *topBorderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, numberPadAccessoryInputView.frame.size.width, 1.0f)];
topBorderView.backgroundColor = [UIColor whiteColor];
[numberPadAccessoryInputView addSubview:topBorderView];
// Make it so that this UITextField shows the UIToolbar
self.textField.inputAccessoryView = numberPadAccessoryInputView;
就這樣,有一個漂亮的,漂亮的,直觀的控制添加到清楚地表明,當他們與他們的文本輸入完成後,用戶應如何進行鍵盤的頂部。
我仍然相信蘋果應該爲這個鍵盤(如發表的Janak尼爾默爾的鏈接),但是這是最優雅的非蘋果的解決方案給我提供了一個「完成」按鈕。
超級光滑。謝謝! – Andy
爲SWIFT使用下面的代碼
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
textFiled.resignFirstResponder()
}
退房最新更新對以下Link
謝謝。很高興在這裏看到一個迅速的答案 –
歡迎@ZackShapiro –
'touchesBegan'已更新。如果你有問題,結帳http://stackoverflow.com/questions/28771896/overriding-method-with-selector-touchesbeganwithevent-has-incompatible-type。 – user3731622
很多,但這裏仍答案不得不努力使用的XCode 5 閱讀本弄明白: https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html
添加敲擊手勢識別器到您的UIScrollView。設置其代表是視圖 - 控制。
此方法添加:
- (IBAction)tappedSomewhere:(id)sender {
[self.view endEditing:YES];
}
您的UIScrollView實現代碼,它通過控制點擊拖動到實現代碼關聯到敲擊手勢識別器。
爲我工作。
對於斯威夫特,最容易通過的文本字段以外的任何地方攻駁回了所有文本字段鍵盤是:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
self.view.endEditing(true)
}
必須使用UITapGestureRecognizer實現這一目標。
UITapGestureRecognizer *tapToDismiss = [[UITapGestureRecognizer alloc]initWithTarget: self action: @selector (dismissNumberKeyBoard:)];
然後在您的dismissNumberKeyboard方法,
-(Void)dismissNumberKeyboard : (id)sender {
[yourTextField resignFirstResponder];
}
編碼愉快!
您可以通過將一個didSet
在出口和有附加的inputAccessoryView實現@ MBM的回答真的很好:
斯威夫特代碼:
@IBOutlet var input: UITextField! { didSet {
let toolbar = UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 0, height: 44)))
toolbar.items = [
UIBarButtonItem(barButtonSystemItem: .done, target: self.input,
action: #selector(resignFirstResponder))
]
input.inputAccessoryView = toolbar
}}
在Xcode 8/iOS的10它結束了找東西像這樣:
SWIFT的3/4,我喜歡這種情況下:
- 視圖控制器子類UITextFieldDelegate
- 在viewDidLoad中的功能,你必須設置
yourPhonePadField.delegate = self
並覆蓋該功能:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) }
- 1. 當點擊數字鍵盤關閉鍵盤,並把號碼
- 2. 關閉鍵盤數字鍵盤
- 3. 如何關閉iOS鍵盤?
- 4. 如何關閉鍵盤?
- 5. 關閉數字鍵盤式鍵盤而不添加完成鍵
- 6. 如何通過編程方式從textview中關閉鍵盤
- 7. 如何通過點擊鍵盤箭頭來改變圖像?
- 8. resignFirstResponder並從任何地方關閉鍵盤?
- 9. 如何判斷何時從UIWebview鍵盤的鍵盤上點擊?
- 10. Obj-C - 通過在UITextView屏幕上的任意位置點擊關閉鍵盤?
- 11. 在設置包中關閉鍵盤/數字鍵盤iOS
- 12. UIKeyboardTypeNumberPad關閉鍵盤
- 13. Android關閉鍵盤
- 14. MonoTouch.Dialog:關閉鍵盤
- 15. MFMessageComposeViewController關閉鍵盤
- 16. 關閉iphone鍵盤
- 17. 通過鍵盤
- 18. 鍵盤上點擊
- 19. 如何關閉十位數字後的數字鍵盤?
- 20. Windows 8 - 如何關閉觸摸鍵盤?
- 21. 如何在Android SearchView中關閉鍵盤?
- 22. 如何失去而不關閉鍵盤
- 23. 如何在TimePicker關閉鍵盤圖標?
- 24. 如何從視圖中關閉鍵盤?
- 25. 如何同時關閉UIAlertController和鍵盤?
- 26. 關閉在桌面上敲擊鍵盤
- 27. 通過硬盤搜索關鍵字
- 28. 如何使我的點擊可以通過鍵盤選項卡
- 29. iPhone:可以通過UITextField清除按鈕來關閉鍵盤嗎?
- 30. 關閉輸入焦點的Android鍵盤
如果你想要把完成按鈕在數字按鈕,您可以按照此鏈接http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key –