1

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);
        }
    }];

}
  • The code looks familiar. Isn't it just the matter of adjusting the Y value depending on the device orientation? – El Tomato Apr 19 '13 at 13:03

2 Answers2

1

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);
    }
}];
}
user1459524
  • 3,613
  • 4
  • 19
  • 28
  • That worked. Just had to swap the 72 and -72. Do you know what method would change height and width? –  Apr 24 '13 at 07:00
  • I guess you could use CGAffineTransformScale. Besides Apple's own reference (http://developer.apple.com/library/ios/ipad/#documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html) I would suggest this good post about CGAffineTransform, it helped me a lot to get my mind around it: http://iphonedevelopment.blogspot.com/2008/10/demystifying-cgaffinetransform.html – user1459524 Apr 24 '13 at 22:39
  • This is not working on iOS8 when I have the modal dialog contained in a UIViewController. The nav controller just sort of jumps up a bit and jumps back down. – Chris Prince Sep 21 '14 at 07:58
0

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 
    }];
}
beryllium
  • 29,669
  • 15
  • 106
  • 125
CharlieReed
  • 126
  • 1
  • 6