IOS TextFiled and TextView Keyboard Stowing and Handling Keyboard Occlusion

  • 2021-09-12 02:28:22
  • OfStack

IOS TextFiled and TextView Keyboard Stowing and Handling Keyboard Occlusion

In the development of iOS, UITextFiled and UITextView are two very common controls. When we set these two controls, click on the text input area, and the system will automatically pop up the keyboard. But how to put up the keyboard, where to click to put up the keyboard, and how to block the input box after the keyboard pops up in iPhone4?

This article will lead you to solve:

1 "Click on other blank areas to fold the keyboard
2 "Click the key in the lower right corner of the keyboard to fold the keyboard
3 "Dealing with keyboard occlusion

1. Click on other blank areas to put away the keyboard


- (void)viewDidLoad { 
  [super viewDidLoad]; 
 
  [self setUpForDismissKeyboard];   
} 


#pragma mark -  Reclaim 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){ 
          [self.view addGestureRecognizer:singleTapGR]; 
        }]; 
  [nc addObserverForName:UIKeyboardWillHideNotification 
          object:nil 
           queue:mainQuene 
        usingBlock:^(NSNotification *note){ 
          [self.view removeGestureRecognizer:singleTapGR]; 
        }]; 
} 
 
- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer { 
  // This method Will self.view All the things in the subview Adj. first responder All resign Drop  
  [self.view endEditing:YES]; 
} 

2. Click the key in the lower right corner of the keyboard to fold the keyboard


#pragma mark - TextView  Proxy method  
 
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
  if ([text isEqualToString:@"\n"]) { 
     
    [self.workLogTextView resignFirstResponder]; 
     
    return NO; 
  } 
   
  return YES; 
} 

Note: Proxies that comply with textView/textFiled are required. Change the code is textView proxy method, if the actual use of textFiled, just call textFiled this kind of method can be.

3. Deal with keyboard occlusion


#pragma mark  Keyboard occlusion  
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView { 
  if (self.userInfo.isPhone4) { 
    CGFloat offset_y = 0.f; 
    if (textView.tag == CALL_CONTENT_TEXTFIRLD) { 
      offset_y = 100.f; 
    } 
    CGPoint point = self.BackScrollView.contentOffset; 
    point = CGPointMake(point.x, offset_y); 
    [UIView animateWithDuration:0.25 animations:^{ 
      self.BackScrollView.contentOffset = point; 
    }]; 
  } 
  return YES; 
} 
 
 
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{ 
  if (self.userInfo.isPhone4) { 
    CGFloat offset_y = 0.f; 
    if (textView.tag == CALL_CONTENT_TEXTFIRLD) { 
      offset_y = 100.f; 
    } 
    CGPoint point = self.BackScrollView.contentOffset; 
    point = CGPointMake(point.x, 0); 
    [UIView animateWithDuration:0.25 animations:^{ 
      self.BackScrollView.contentOffset = point; 
    }]; 
  } 
  return YES; 
} 

Note: Proxies that comply with UIScrollViewDelegate and textView/textFiled are required. The parent view of the page is required to be UIScrollView to ensure that the page moves up when the keyboard is ejected and down when the keyboard is folded. self. BackScrollView in the code is the corresponding parent view. Please replace it when using it.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: