Keyboard occlusion processing keyboard handler move up under iOS autolayout

  

In ios, since the keyboard will block the area below the screen after it appears, there will be a need to raise the view below when the keyboard appears.

 

The previous method of calculating x,y under autolayout probably won't work properly, a new solution is needed

 

 

The basic idea of ​​the new method is to adjust the bottomConstraint of the view that needs to be moved up, and let him add the height of the keyboard

 

 

The specific logic is

 

1. Listen to the keyboard event UIKeyboardWillShowNotification

2. Get the keyboard height, pay attention to get the value of UIKeyboardFrameEndUserInfoKey, other values ​​may be OK on the English keyboard, but there will be an error on the Chinese keyboard

3. Improve the bottomConstraint of the target view. It should be noted that the height of the keyboard will change during rotation, so pay attention to updating

 

 

code above

   [[NSNotificationCenter defaultCenter] addObserverForName: UIKeyboardWillShowNotification
     
                                                      object: nil
                                                       queue: nil
                                                  usingBlock: ^(NSNotification *note){
         NSDictionary* userInfo = note.userInfo;
         CGRect keyboardFrame = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
         //keyboardSlideDuration is an instance variable so we can keep it around to use in the "dismiss keyboard" animation.
         keyboardSlideDuration = [[userInfo objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];
         
         // keyboard height will change during rotation, so bottomConstraint need to keep update
         if (self.bottomConstraint.constant != keyboardFrame.size.height) {
             //             CLog(@"move up");
             self.bottomConstraint.constant = keyboardFrame.size.height;
             
             [UIView animateWithDuration: keyboardSlideDuration
                                   delay: 0
                                 options: 0
                              animations:^{
                                  [self.view layoutIfNeeded];
                              }
                              completion: nil
              ];
         }
         
     }
     ];
    
    [[NSNotificationCenter defaultCenter] addObserverForName: UIKeyboardWillHideNotification
                                                      object: nil
                                                       queue: nil
                                                  usingBlock: ^(NSNotification *note){
          self.bottomConstraint.constant = 0;
          [UIView animateWithDuration: keyboardSlideDuration
                                delay: 0
                              options: 0
                           animations:
                                       ^{
                                           [self.view layoutIfNeeded];
                                                                                                  
                                       }
                           completion: nil];
        }
     ];
    

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326116822&siteId=291194637