I've UIView - UIScrollView - UITextField
If keyboard appears (textField delegate), I move the self.view up with some offset value. But how is that the same value makes different offset in LandscapeLeft and LandscapeRight? In LandscapeLeft it's work right(picture on bottom). Seems like there's 20px difference (big coincidence with statusBar height?)
How to fix that? ( hardcoding +20 is not accepted as answer ) Thanks for help.
// AHTextFieldHelper.m
...
- (void)moveViewUp:(UIView *)view withOffset:(int)offset forOrientation:(Orientation)orientation withDuration:(float)animationDuration
{
_animationDuration = animationDuration;
_view = view;
_offset = offset;
_orientation = orientation;
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
if (orientation == portrait)
{
_tempPortraitRect = view.frame;
view.frame = CGRectOffset(view.frame, view.frame.origin.x, view.frame.origin.y - offset);
}
else if (orientation == landscapeLeft)
{
_tempLandscapeLeftRect = view.frame;
view.frame = CGRectOffset(view.frame, view.frame.origin.x + offset, view.frame.origin.y);
}
else if (orientation == landscapeRight)
{
_tempLandscapeRightRect = view.frame;
view.frame = CGRectOffset(view.frame, view.frame.origin.x - offset, view.frame.origin.y);
}
} completion:^(BOOL finished){
_isUP = YES;
}];
}
ViewController.m - (void)textFieldDidBeginEditing:(UITextField *)textField
// ViewController.m - (void)textFieldDidBeginEditing:(UITextField *)textField
// some refactoring needed
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
_originalLandscapeRightFrame = self.view.frame;
CGPoint translatedTextFieldOrigin = [[textField superview] convertPoint:textField.frame.origin toView:self.view];
if (fabs(translatedTextFieldOrigin.y) >= ([RSHelpersFunctions screenResolution].width - kAHiPhoneLandscapeKeyboardHeight) - textField.frame.size.height)
{
CGFloat newPoint = (translatedTextFieldOrigin.y - ([RSHelpersFunctions screenResolution].width - kAHiPhoneLandscapeKeyboardHeight)) + textField.frame.size.height + 30 + 20; // quick fix
_textFieldHelper = [AHTextFieldHelper new];
[_textFieldHelper moveViewUp:self.view withOffset:fabs(newPoint) forOrientation:landscapeRight withDuration:0.5];
}
}
else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
{
_originalLandscapeLeftFrame = self.view.frame;
CGPoint translatedTextFieldOrigin = [[textField superview] convertPoint:textField.frame.origin toView:self.view];
if (fabs(translatedTextFieldOrigin.y) >= ([RSHelpersFunctions screenResolution].width - kAHiPhoneLandscapeKeyboardHeight) - textField.frame.size.height)
{
CGFloat newPoint = (translatedTextFieldOrigin.y - ([RSHelpersFunctions screenResolution].width - kAHiPhoneLandscapeKeyboardHeight)) + textField.frame.size.height + 30;
_textFieldHelper = [AHTextFieldHelper new];
[_textFieldHelper moveViewUp:self.view withOffset:fabs(newPoint) forOrientation:landscapeLeft withDuration:0.5];
}
}
The top level UIView in the view hierarchy has its frame non-adjusted for orientation of the device. You may have better fortune trying to work with the bounds for the top-level view instead (which is adjusted for orientation).
To see this in action, try looking at the output produced by the following:
NSLog(@"self.view.frame = %@", NSStringFromCGRect(self.view.frame));
NSLog(@"self.view.bounds = %@", NSStringFromCGRect(self.view.bounds));
You will probably see the width and height for frame versus bounds being 'swapped'.
Another strategy is to have a UIView nested inside your top-level view, with correct autoresize/struts set, and then work with nested view's frame instead of the top-level frame.