Desarrollo de iOS: haga clic en el espacio en blanco para salir del teclado

1. Métodos utilizados anteriormente para salir del teclado.

Si hay un UITextField en UIScrollView, no es válido usar el método touchesBegan directamente para finalizar la edición (salir del teclado). Debe agregar otra categoría a UIScrollView y reescribir varios métodos.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}

Este método se usa generalmente, pero hay un problema al escribir de esta manera: se producirá un problema de falla al usar el teclado de escritura a mano del sistema. _Al
escribir a mano la entrada del teclado, UIScrollView's - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; este método de clasificación, el tipo de self es UIKBCandidateCollectionView, un tipo no expuesto por el sistema, debería ser una subclase de UIScrollView, por lo que el sistema fallará.

2. Aumentar el juicio para evitar fallas al escribir a mano

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesBegan:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesBegan:touches withEvent:event];
        }
    }
    
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesMoved:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesMoved:touches withEvent:event];
        }
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesEnded:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesEnded:touches withEvent:event];
        }
    }
}

No hay ningún problema con UIscrollview con este método, pero cancelar el teclado en UItableview fallará.

3. Haga clic en el gesto para salir del teclado.

Utilice gestos de toque para evitar problemas

UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchViewCancelKeyBoard:)];
    tapView.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapView];
- (void)touchViewCancelKeyBoard:(UITapGestureRecognizer *)tap {
    [self.TextField resignFirstResponder];
}

4. Las propiedades que vienen con la vista de desplazamiento.

scsrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

Entre ellos, KeyboardDismissMode es una propiedad de UIScrollView,
su valor se suma a UIScrollViewKeyboardDismissModeNone y
UIScrollViewKeyboardDismissModeOnDrag, lo que significa que el teclado desaparece al arrastrar, y
UIScrollViewKeyboardDismissModeInteractive, lo que significa que el teclado puede moverse fuera de la pantalla cuando el dedo se desliza hacia abajo.

Espero que este contenido sea útil para todos.

Supongo que te gusta

Origin blog.csdn.net/guoxulieying/article/details/117438308#comments_18148682
Recomendado
Clasificación