Skip to content

Instantly share code, notes, and snippets.

@jamesu
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesu/568ee341f829ebf01421 to your computer and use it in GitHub Desktop.
Save jamesu/568ee341f829ebf01421 to your computer and use it in GitHub Desktop.
Moving a UIViewController view to line up with the keyboard
// Moves view to be in line with the keyboard
//
// keyboardHeight == value returned from UIKeyboardBoundsUserInfoKey key
// keyboardControlY == place in view the top of the keyboard should line up with
//
- (void)moveViewForKeyboardHeight:(float)keyboardHeight controlOffset:(float)keyboardControlY
{
// Determine what is "up"
CGAffineTransform myTransform = self.transform;
CGPoint upAxis = CGPointApplyAffineTransform(CGPointMake(0, 1), myTransform);
switch (_deviceOrientation)
{
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
if (!portrait) {
// make sure control is JUST ABOVE keyboard
CGPoint transformMul;
if (upAxis.x > 0) // flip on ios < 8
{
transformMul = CGPointMake(-upAxis.x, -upAxis.y);
}
else // up is up
{
transformMul = CGPointMake(-upAxis.x, -upAxis.y);
}
if (keyboardHeight > 0)
{
cy = -(keyboardControlY) + keyboardHeight;
if (cy < 0)
cy = 0;
}
myTransform.tx = cy * transformMul.x;
myTransform.ty = cy * transformMul.y;
}
break;
default:
break;
}
self.transform = myTransform;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment