iOS handles keyboard block TextField, TextView problem

  Before handling the keyboard occlusion are treated separately in each controller, this is really very troublesome, today doing the project when he thought of his package a record about this "ups and downs" process.

  The idea is this: calculating text edit control keyboard Frame Frame, if the mobile shutter controller View.

  Create a controller class: WKAvoidKeyboardViewController

  

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface WKAvoidKeyboardViewController : UIViewController
 4 
 5 @property (nonatomic, strong) UITextField *editTextField;
 6 @property (nonatomic, strong) UITextView *editTextView;
 7 
 8 - (void)hideKeyboard:(NSNotification *)noti;
 9 - (void)showKeyboard:(NSNotification *)noti;
10 
11 
12 @end
13 
14 
15 #import "WKAvoidKeyboardViewController.h"
16 
17 #define GetOSVersion [[UIDevice currentDevice].systemVersion floatValue]
18 
19 #define GetTransformDistance(Distance) (GetOSVersion < 7.1 ? Distance / 2 : Distance)
20 
21 @interface WKAvoidKeyboardViewController ()<UITextFieldDelegate, UITextViewDelegate>
22 
23 @end
24 
25 @implementation WKAvoidKeyboardViewController
26 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
27 {
28     [self.view endEditing:YES];
29 }
30 @end

 

  Step 1: Get the currently edited text controls by notice

//注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
- (void)textFieldBeginEditing:(NSNotification *)noti
{
    self.editTextField = noti.object;
    self.editTextView = nil; 
}
- (void)textViewBeginEditing:(NSNotification *)noti
{
    self.editTextView = noti.object;
    self.editTextField = nil;
}

 

  Step 2: Notify get the keyboard height

  Step 3: Calculate the need for moving

  

#pragma mark - 键盘躲避

- (void)showKeyboard:(NSNotification *)noti
{
    self.view.transform = CGAffineTransformIdentity;
    UIView *editView = _editTextView ? _editTextView : _editTextField;
    
    CGRect tfRect = [editView.superview convertRect:editView.frame toView:self.view];
    NSValue *value = noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"];
    NSLog(@"%@", value);
    CGRect keyBoardF = [value CGRectValue];
    
    CGFloat animationTime = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    CGFloat _editMaxY = CGRectGetMaxY(tfRect);
    CGFloat _keyBoardMinY = CGRectGetMinY(keyBoardF);
    NSLog(@"%f %f", _editMaxY, _keyBoardMinY);
    if (_keyBoardMinY < _editMaxY) {
        CGFloat moveDistance = _editMaxY - _keyBoardMinY;
        [UIView animateWithDuration:animationTime animations:^{
            self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -moveDistance);
        }];
        
    }
}

- (void)hideKeyboard:(NSNotification *)noti
{
    //    NSLog(@"%@", noti);
    self.view.transform = CGAffineTransformIdentity;
}

  Preliminary tests: UITextFiled success, and then to the UITextView, the issue pits father thick line = =, UITextViewTextDidBeginEditingNotification transmission time is after the keyboard pop-up notifications, leading to the first click TextView does not work, click a second time to produce results. Ever since, I began to try to do with the TextView Delegate, granted proxy method used

- (void)textViewDidBeginEditing:(UITextView *)textView

1 - (void)textViewDidBeginEditing:(UITextView *)textView
2 {
3     
4 }

  It is disappointing that textViewDidBeginEditing: method call is still called in the notification pop-up keyboard before, this time in mind is: beep dog, and people how to play! Or look at it the other methods. So to see the proxy method

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

  After the attempt, this approach does at the keyboard pops up call, you're done, the next step is to set the question of agency

  Agent set as follows:

 1 - (void)searchTextViewWithView:(UIView *)view
 2 {
 3     for (UIView *subview in view.subviews)
 4     {
 5         if ([subview isKindOfClass:[UITextView class]]) {
 6             ((UITextView *)subview).delegate = self;
 7         }
 8         if ([subview isKindOfClass:[UITextField class]]) {
 9             ((UITextField *)subview).delegate = self;
10         }
11         [self searchTextViewWithView:subview];
12     }
13 }

  At this point you're done, use: inheritance WKAvoidKeyboardViewController, if it is created with text control version of the story, and consequently do not do, if the code is created, you need to call a method in ViewDidLoad in searchTextViewWithView

  Download the complete code: https://github.com/WuKongCoo1/AvoidKeyboardDemo.git

 

Reproduced in: https: //www.cnblogs.com/pretty-guy/p/4794002.html

Guess you like

Origin blog.csdn.net/weixin_33893473/article/details/93199940