How to dismiss number pad keyboard by tapping anywhere

后端 未结 15 2188
梦如初夏
梦如初夏 2020-12-04 17:53

I\'d like to know the simplest code to dismiss the number pad keyboard when tapping anywhere outside the number pad. It\'s a simple application to input a number inside a te

15条回答
  •  误落风尘
    2020-12-04 18:13

    If you've made a custom number pad or other popover, another solution would be to use a gesture recognizer in the init of your UIView like this:

    tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    tapper.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapper];
    
     _yourViewController = [[CustomViewController alloc] init];
    _yourPopoverController = [[UIPopoverController alloc] initWithContentViewController:_yourViewController];
    _yourPopoverController.popoverContentSize = CGSizeMake(550, 300);
    _yourViewController.delegate = self;
    

    And have the handleSingleTap dismiss the popover if visible and dismiss editing:

    - (void)handleSingleTap:(UITapGestureRecognizer *) sender
    {
        if ([_yourPopoverController isPopoverVisible]) {
            [_yourPopoverController dismissPopoverAnimated:YES];
        }
    
        [self endEditing:YES];
    }
    

提交回复
热议问题