Last active
August 29, 2015 14:06
-
-
Save jamesu/568ee341f829ebf01421 to your computer and use it in GitHub Desktop.
Moving a UIViewController view to line up with the keyboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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