Hide keyboard on touch outside of textfield

2019-04-01 23:55发布

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)

6条回答
我想做一个坏孩纸
2楼-- · 2019-04-02 00:32

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-04-02 00:36

What I do, is change the overall UIView class to UIControl. enter image description here

This gives you a touchDown event you can link up to a method to resignFirstResponder. enter image description here
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
查看更多
Emotional °昔
4楼-- · 2019-04-02 00:38

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.

查看更多
放荡不羁爱自由
5楼-- · 2019-04-02 00:46
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楼-- · 2019-04-02 00:51

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.

查看更多
唯我独甜
7楼-- · 2019-04-02 00:51
- (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];

}
查看更多
登录 后发表回答