How to resign first responder from text field when user tap elsewhere?

前端 未结 18 3106
野趣味
野趣味 2020-12-14 02:12

I have filled my view with ScrollView (same size as the view) and I\'m stuck at how to resign first responder when user tap elsewhere in the View (or the scrollview). Any id

18条回答
  •  萌比男神i
    2020-12-14 02:25

    For a more robust and clean solution add a tap gesture recognizer to your primary view.

    This will work better with nested views and will be cleaner than secret buttons in code and UI builder.

    In your view did load:

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

    ..and define your target action to be triggered on tap:

    -(void) dismissKeyboard:(id)sender
    {
        [self.view endEditing:YES];
    }
    

提交回复
热议问题