IOS TextField and TextView keyboards are closed and handled with keyboard occlusion
In iOS development, UITextFiled and UITextView are two very common controls. When we set these two controls and click on the text input area, the system will automatically pop up the keyboard. But how to put the keyboard away, where to put the keyboard away, and what to do if the keyboard pops up in iPhone 4?
This article will lead you to solve:
1》Click on other blank areas to close the keyboard
2》Click the key in the lower right corner of the keyboard to close the keyboard
3》Troubleshooting the keyboard occlusion problem
1. Click on other blank areas to close the keyboard
- (void)viewDidLoad { [super viewDidLoad]; [self setUpForDismissKeyboard]; }
#pragma mark - Recycle any blank area keyboard events- (void)setUpForDismissKeyboard { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; UITapGestureRecognizer *singleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnywhereToDismissKeyboard:)]; NSOperationQueue *mainQuene =[NSOperationQueue mainQueue]; [nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQuene usingBlock:^(NSNotification *note){ [ addGestureRecognizer:singleTapGR]; }]; [nc addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQuene usingBlock:^(NSNotification *note){ [ removeGestureRecognizer:singleTapGR]; }]; } - (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer { //This method will resign all the first responders of the subview in it [ endEditing:YES]; }
2. Click the key in the lower right corner of the keyboard to close the keyboard
#pragma mark - TextView proxy method -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [ resignFirstResponder]; return NO; } return YES; }
Note: The proxy that complies with textView/textFiled is required. The code changes are textView proxy method. If you actually use textFiled, just call the method of textFiled.
3. Handle keyboard occlusion problem
#pragma mark keyboard occlusion- (BOOL)textViewShouldBeginEditing:(UITextView *)textView { if (.isPhone4) { CGFloat offset_y = ; if ( == CALL_CONTENT_TEXTFIRLD) { offset_y = ; } CGPoint point = ; point = CGPointMake(, offset_y); [UIView animateWithDuration:0.25 animations:^{ = point; }]; } return YES; } - (BOOL)textViewShouldEndEditing:(UITextView *)textView{ if (.isPhone4) { CGFloat offset_y = ; if ( == CALL_CONTENT_TEXTFIRLD) { offset_y = ; } CGPoint point = ; point = CGPointMake(, 0); [UIView animateWithDuration:0.25 animations:^{ = point; }]; } return YES; }
Note: A proxy that complies with UIScrollViewDelegate and textView/textFiled is required. The parent view of this page is required to be a UIScrollView to ensure that the page moves upward when the keyboard pops up, and the page moves downward when the keyboard is closed. The corresponding parent view in the code is, please replace it when using it.
Thank you for reading, I hope it can help you. Thank you for your support for this site!