0

我有 UIView - UIScrollView - UITextField

如果出现键盘(textField 委托),我将 self.view 向上移动一些偏移值。但是,相同的值如何在 LandscapeLeft 和 LandscapeRight 中产生不同的偏移量?在 LandscapeLeft 中,它工作正常(底部图片)。似乎有 20px 的差异(与 statusBar 高度重合?)

如何解决?(不接受硬编码+20作为答案)感谢您的帮助。

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

在此处输入图像描述

4

1 回答 1

0

视图层次结构中的顶级 UIView 的框架未针对设备的方向进行调整。您可能会更幸运地尝试使用顶级视图的边界(针对方向进行了调整)。

要查看实际情况,请尝试查看以下生成的输出:

NSLog(@"self.view.frame = %@", NSStringFromCGRect(self.view.frame));
NSLog(@"self.view.bounds = %@", NSStringFromCGRect(self.view.bounds));

您可能会看到框架与边界的宽度和高度被“交换”。

另一种策略是将 UIView 嵌套在您的顶级视图中,并设置正确的自动调整大小/支柱,然后使用嵌套视图的框架而不是顶级框架。

于 2013-03-12T13:12:21.087 回答