The modal dialog gets moved up when they keyboard appears and moves down when the keyboard disappears.
All is fine till I rotate the iPad. In any other orientation except the standard it doesn't work. When the iPad is turned around the modal dialog moves down when the keyboard appears instead of up and up when the keyboard disappears instead of down.
This is the code I am using to position the modal dialog when 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);
}
}];
}
Instead of setting the frame, use CGAffineTransformTranslate, for example like so:
- (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);
}
}];
}
You should try using Keyboard Notifications:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeDismissed:) name:UIKeyboardWillHideNotification object:nil];
and then in the selectors adjust the frame. 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
}];
}