Answers:
您需要添加一个UITapGestureRecogniser
并将其分配给视图,然后UITextField
在其选择器上调用resign第一响应者。
编码:
在viewDidLoad中
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
在dismissKeyboard中:
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
}
(哪里aTextField
是负责键盘的文本字段)
Swift 3版本看起来像这样
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
self.view.addGestureRecognizer(tapGesture)
用于关闭键盘
func dismissKeyboard (_ sender: UITapGestureRecognizer) {
aTextField.resignFirstResponder()
}
[aTextField resignFirstResponder]
并且可以[self.view endEditing:YES];
在多个文本字段中使用
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(endEditing:)]];
[tap setCancelsTouchesInView:NO];
@qiaoyi的答案中添加了@ParthBhatt;我有一个表不响应行选择的问题。5年后,我希望这对其他人有帮助。
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
我混了几个答案。
使用在执行期间初始化的ivar viewDidLoad:
UIGestureRecognizer *tapper;
- (void)viewDidLoad
{
[super viewDidLoad];
tapper = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSingleTap:)];
tapper.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapper];
}
取消当前正在编辑的内容:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}
检查一下,这将是最简单的方法,
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];// this will do the trick
}
要么
该库将处理包括滚动条自动滚动,点击空间以隐藏键盘等。
我看到有些人在使用该UITapGestureRecognizer
方法时遇到问题。在保持原样的现有按钮的点击行为不变的情况下实现此功能的最简单方法是,在@ Jensen2k的答案中仅添加一行:
[tap setCancelsTouchesInView:NO];
这使我现有的按钮仍然可以使用@Dmitry Sitnikov的方法来工作。
property
在这里阅读有关内容(搜索CancelsTouchesInView
):UIGestureRecognizer类参考
我不确定它如何与滚动条一起使用,因为我看到一些问题,但希望其他人可能会遇到与我相同的情况。
UIImageView
,在视图的背面具有背景图像的用户,我还要添加,[imageView setUserInteractionEnabled:NO];
然后单击“单击”,这使此方法有效。
最好使您UIView
的实例成为实例UIControl
(在界面生成器中),然后将其TouchUpInside
事件连接到dismissKeyboard
方法。该IBAction
方法如下所示:
- (IBAction)dismissKeyboard:(id)sender {
[aTextBox resignFirstResponder];
}
UITapGestureRecognizer
干扰了点击视图内部的按钮)。touchesEnded
之所以无法正常运作,是因为UIScrollView
响应链混乱了。)
斯威夫特4
设置你的UIViewController
这种扩展方法一旦如viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
self.setupHideKeyboardOnTap()
}
并且即使轻按键盘也会关闭键盘NavigationBar
。
import UIKit
extension UIViewController {
/// Call this once to dismiss open keyboards by tapping anywhere in the view controller
func setupHideKeyboardOnTap() {
self.view.addGestureRecognizer(self.endEditingRecognizer())
self.navigationController?.navigationBar.addGestureRecognizer(self.endEditingRecognizer())
}
/// Dismisses the keyboard from self.view
private func endEditingRecognizer() -> UIGestureRecognizer {
let tap = UITapGestureRecognizer(target: self.view, action: #selector(self.view.endEditing(_:)))
tap.cancelsTouchesInView = false
return tap
}
}
Swift版本,可与其他元素(例如UIButton
或UITextField
)结合使用:
override func viewDidLoad() {
super.viewDidLoad()
let tapper = UITapGestureRecognizer(target: self, action:#selector(endEditing))
tapper.cancelsTouchesInView = false
view.addGestureRecognizer(tapper)
}
self
而不是self.view
canclesTouchesInView
是一个方便使用的属性。谢谢。
c#
。仅供参考,分号是允许使用语法的,只是没有必要。
怎么样:我知道这是一个旧帖子。它可能会帮助某人:)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *subviews = [self.view subviews];
for (id objects in subviews) {
if ([objects isKindOfClass:[UITextField class]]) {
UITextField *theTextField = objects;
if ([objects isFirstResponder]) {
[theTextField resignFirstResponder];
}
}
}
}
斯威夫特4 oneliner
view.addGestureRecognizer(UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:))))
我认为最简单(也是最好)的方法是对全局视图进行子类化,并使用hitTest:withEvent
方法来监听任何触摸。键盘上的触摸未注册,因此仅当您在其他地方触摸/滚动/滑动/捏...时才调用hitTest:withEvent [self endEditing:YES]
。
这比使用更好,touchesBegan
因为touchesBegan
如果单击视图顶部的按钮不会被调用。例如,它比UITapGestureRecognizer
不能识别滚动手势要好。它也比使用暗屏更好,因为在复杂而动态的用户界面中,无法将暗屏无处不在。此外,它不会阻止其他操作,您无需点按两次即可选择外部的按钮(例如在的情况下UIPopover
)。
另外,它比调用更好[textField resignFirstResponder]
,因为屏幕上可能有许多文本字段,所以这对所有这些都有效。
这必须是通过触摸外部来隐藏键盘的最简单方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
您可以使用XCode 6及更高版本中的情节提要进行此操作:
将此添加到您的ViewController使用的类的头文件中:
@interface TimeDelayViewController : UIViewController <UITextFieldDelegate>
- (IBAction)dissmissKeyboardOnTap:(id)sender;
@end
然后将其添加到同一ViewController的实现文件中:
- (IBAction)dissmissKeyboardOnTap:(id)sender{
[[self view]endEditing:YES];
}
现在,这将是故事板场景(即ViewController)的“已接收动作”之一:
现在,您需要将此操作与触发键盘的用户手势联系起来。
重要提示-您需要将情节提要中包含的“ UIView”转换为UIControl,以便它可以接收事件。从“视图控制器场景”层次结构中选择视图:
...并更改其类别:
现在,从场景的“已接收动作”旁边的小圆圈拖动到场景的“空”部分(实际上是将“已接收动作”拖动到UIControl)。系统将显示一系列事件,您可以将其关联到以下操作:
选择“内部润饰”选项。现在,您已经将创建的IBAction钩接到了用户触摸键盘的操作上。当用户点击键盘时,它将被隐藏。
(注意:要将动作与事件挂钩,也可以将接收到的动作直接拖到View Controllers层次结构中的UIControl上。在层次结构中显示为“ Control”。)
如果我没错,您希望在外部轻按辞职键盘,textfield
但您没有参考资料textfield
。
尝试这个;
reftextField
现在在textFieldDidBeginEditing
设置引用文本字段为
- (void) textFieldDidBeginEditing:(UITextField *)textField{
reftextField = textField;
}
现在您可以愉快地在任何按钮时钟上使用(建议在开始编辑时添加透明按钮)
- (void)dismissKeyboard {
[reftextField resignFirstResponder];
}
或为完成辞职按钮尝试此操作。
//for resigning on done button
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
只是为了在此处添加我的版本,该版本介绍了如何在外部触摸时关闭键盘。
viewDidLoad:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTap];
任何地方:
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
[textFieldName resignFirstResponder];
puts("Dismissed the keyboard");
}
这里有很多有关使用-的好答案UITapGestureRecognizer
,所有这些都打破了UITextField
clear(X)按钮。解决方案是通过其委托抑制手势识别器:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
BOOL touchViewIsButton = [touch.view isKindOfClass:[UIButton class]];
BOOL touchSuperviewIsTextField = [[touch.view superview] isKindOfClass:[UITextField class]];
return !(touchViewIsButton && touchSuperviewIsTextField);
}
这不是最可靠的解决方案,但对我有用。
return !touchViewIsButton
。我猜想其他任何需要关闭键盘的按钮也应该在其操作中这样做。此外,在键盘关闭时更改布局时可能不会调用TouchUpInside。
@ Jensen2k的Swift版本的答案:
let gestureRecognizer : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(gestureRecognizer)
func dismissKeyboard() {
aTextField.resignFirstResponder()
}
一支班轮
self.view.addTapGesture(UITapGestureRecognizer.init(target: self, action: "endEditing:"))
我以Barry为例进行新开发。效果很好!但是我必须稍作改动,要求仅关闭正在编辑的文本字段的键盘。
因此,我在Barry示例中添加了以下内容:
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
_textBeingEdited = textField;
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
_textBeingEdited = nil;
}
另外,我更改了hideKeyboard方法,如下所示:
- (IBAction)hideKeyboard:(id)sender
{
// Just call resignFirstResponder on all UITextFields and UITextViews in this VC
// Why? Because it works and checking which one was last active gets messy.
//UITextField * tf = (UITextField *) sender;
[_textBeingEdited resignFirstResponder];
}
最简单,最快捷的方法之一就是将此代码添加到您的 viewDidLoad
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self.view
action:@selector(endEditing:)]];
我在这里尝试了许多回复,但没有运气。UIButtons
即使cancelsTouchesInView
将手势识别器的属性设置为NO ,我的轻击手势识别器也总是导致我在点击时不响应。
这是最终解决问题的方法:
拥有一个ivar:
UITapGestureRecognizer *_keyboardDismissGestureRecognizer;
当文本字段开始编辑时,请设置手势识别器:
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
if(_keyboardDismissGestureRecognizer == nil)
{
_keyboardDismissGestureRecognizer = [[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)] autorelease];
_keyboardDismissGestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:_keyboardDismissGestureRecognizer];
}
}
然后,诀窍在于如何设置dismissKeyboard
方法:
- (void) dismissKeyboard
{
[self performSelector:@selector(dismissKeyboardSelector) withObject:nil afterDelay:0.01];
}
- (void) dismissKeyboardSelector
{
[self.view endEditing:YES];
[self.view removeGestureRecognizer:_keyboardDismissGestureRecognizer];
_keyboardDismissGestureRecognizer = nil;
}
我想这只是将dismissKeyboardSelector
执行从触摸处理执行堆栈中移出的事情...
在此示例中,aTextField是唯一的UITextField。
// YourViewController.h
// ...
@interface YourViewController : UIViewController /* some subclass of UIViewController */ <UITextFieldDelegate> // <-- add this protocol
// ...
@end
// YourViewController.m
@interface YourViewController ()
@property (nonatomic, strong, readonly) UITapGestureRecognizer *singleTapRecognizer;
@end
// ...
@implementation
@synthesize singleTapRecognizer = _singleTapRecognizer;
// ...
- (void)viewDidLoad
{
[super viewDidLoad];
// your other init code here
[self.view addGestureRecognizer:self.singleTapRecognizer];
{
- (UITapGestureRecognizer *)singleTapRecognizer
{
if (nil == _singleTapRecognizer) {
_singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapToDismissKeyboard:)];
_singleTapRecognizer.cancelsTouchesInView = NO; // absolutely required, otherwise "tap" eats events.
}
return _singleTapRecognizer;
}
// Something inside this VC's view was tapped (except the navbar/toolbar)
- (void)singleTapToDismissKeyboard:(UITapGestureRecognizer *)sender
{
NSLog(@"singleTap");
[self hideKeyboard:sender];
}
// When the "Return" key is pressed on the on-screen keyboard, hide the keyboard.
// for protocol UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
NSLog(@"Return pressed");
[self hideKeyboard:textField];
return YES;
}
- (IBAction)hideKeyboard:(id)sender
{
// Just call resignFirstResponder on all UITextFields and UITextViews in this VC
// Why? Because it works and checking which one was last active gets messy.
[aTextField resignFirstResponder];
NSLog(@"keyboard hidden");
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSingleTap:)];
[singleTapGestureRecognizer setNumberOfTapsRequired:1];
[singleTapGestureRecognizer requireGestureRecognizerToFail:singleTapGestureRecognizer];
[self.view addGestureRecognizer:singleTapGestureRecognizer];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
[self.view endEditing:YES];
[textField resignFirstResponder];
[scrollView setContentOffset:CGPointMake(0, -40) animated:YES];
}
在这种情况下,可以使用ScrollView并将其添加到TextField
ScrollView中,我想要点击ScrollView
和,然后关闭键盘。我试图创建示例代码以防万一。像这样,
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.tap(_:)))
view.addGestureRecognizer(tapGesture)
// Do any additional setup after loading the view, typically from a nib.
}
func tap(gesture: UITapGestureRecognizer) {
textField.resignFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
您的情节提要板就这样看。
您可以通过在UITextField外部单击来使用UITapGestureRecongnizer方法关闭键盘。通过使用此方法,每当用户在UITextField之外单击时,键盘将被关闭。下面是使用它的代码片段。
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissk)];
[self.view addGestureRecognizer:tap];
//Method
- (void) dismissk
{
[abctextfield resignFirstResponder];
[deftextfield resignFirstResponder];
}