Moving the dialog up when the keyboard is running, except when the ipad is turning

The modal dialog box moves up when the keyboard appears and moves down when the keyboard disappears.

Everything is fine until I turn the iPad. In any orientation other than the standard, it does not work. When the iPad rotates, the modal dialog moves down when the keyboard appears instead of up and up, when the keyboard disappears rather than down.

This is the code that I use to position the modal dialog when the keyboard appears / disappears.

- (void)textFieldDidBeginEditing:(UITextField *)textField { self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 140, self.view.superview.frame.size.width, self.view.superview.frame.size.height); } }]; } -(void)textFieldDidEndEditing:(UITextField *)textField { [UIView animateWithDuration:0.4 animations:^ { self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 212, self.view.superview.frame.size.width, self.view.superview.frame.size.height); } }]; } 
+1
source share
2 answers

Instead of setting a frame, use CGAffineTransformTranslate, for example:

 - (void)textFieldDidBeginEditing:(UITextField *)textField { self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,72); } }]; } -(void)textFieldDidEndEditing:(UITextField *)textField { [UIView animateWithDuration:0.4 animations:^ { self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,-72); } }]; } 
+1
source

You should try using notifications on the keyboard:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeDismissed:) name:UIKeyboardWillHideNotification object:nil]; 

and then adjust the frame in the selectors. Not in textFieldDidBeginEditing / textFieldDidEndEditing.

 - (void)keyboardWasShown:(NSNotification *) notification { NSDictionary *info = [notification userInfo]; NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; CGSize keyboardSize = [aValue CGRectValue].size; keyboardHeight = MIN(keyboardSize.height, keyboardSize.width); // set new frame based on keyboard size - (void)keyboardWillBeDismissed: (NSNotification *) notification{ [UIView animateWithDuration:0.4 animations:^{ // original frame }]; } 
0
source

Source: https://habr.com/ru/post/1203066/


All Articles