iOS - Dismiss UIDatePicker presented as inputView

前端 未结 4 913

I have a text field in my UI that when it\'s selected presents a UIDatePicker instead of the default keyboard, how could I set up a button as to dismiss the picker when the

4条回答
  •  轮回少年
    2021-01-18 05:14

    You can solve this by adding this

    [self.view endEditing:YES];
    

    Add this where you want to dismiss date picker like i have added this on tap gesture
    On viewDidLoad added a Tapgesture

     UITapGestureRecognizer *gesRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; // Declare the Gesture.
        gesRecognizer.delegate = self;
        [self.view addGestureRecognizer:gesRecognizer];
    

    After adding this on viewdidLoad Add this method to detect gesture recognizer

    - (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer{
        NSLog(@"Tapped");
        [self.view endEditing:YES];
    }
    

提交回复
热议问题