Hide keyboard on touch outside of textfield

旧城冷巷雨未停 提交于 2019-12-05 03:09:20

问题


I'm trying to hide the keyboard after a touch anywhere else on the screen. The code I'm using is based on this answer here.

IBOutlet UITextView *myTextView;

And the method:

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

    UITouch *touch = [[event allTouches] anyObject];
    if ([myTextView isFirstResponder] && [touch view] != myTextView) {
        [myTextView resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

What I don't understand is how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.

I also gave this code a try but that one was breaking my navigation buttons (even with the solution mentioned in the comments)


回答1:


Objective C:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     [[self view] endEditing:YES];
}

Swift:

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}

This is the best way I have found and it is very simple.




回答2:


how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.

Because you don't. You have to override this method on the view of which the text field is a subview.




回答3:


What I do, is change the overall UIView class to UIControl.

This gives you a touchDown event you can link up to a method to resignFirstResponder.


The UIControl still gives you all the functionality of a UIView.
-(IBAction)backgroundTap:(id)sender
{
    [text1 resignFirstResponder];
    [text2 resignFirstResponder];
    [textLogin resignFirstResponder];
    [textPassword resignFirstResponder];
} // Resign all responders



回答4:


- (void)viewDidLoad
{
     //for keybord hide when touch outside:


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(hidekeybord)];
    [self.view addGestureRecognizer:tap];



    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


-(void)hidekeybord
{

    [_yourtextfield.txt resignFirstResponder];

}



回答5:


In .h
@property (nonatomic, assign) id currentResponder;

In .h
//in viewDidLoad:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:singleTap];

//Implement the below delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentResponder = textField;
}

//Implement resignOnTap:

- (void)resignOnTap:(id)iSender {
    [self.currentResponder resignFirstResponder];
}



回答6:


Try the quick and dirty way:

Take a button and link it to an action method(lets call it Background). Stretch the button so it covers the whole view. Adjust the layers of the views so only things the user interacts by touch are on top of the button. Change the type of button to custom, this make the button invisible. Dismiss the firstResponder in the method Background.



来源:https://stackoverflow.com/questions/12227859/hide-keyboard-on-touch-outside-of-textfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!