在键盘演示中点击UITextView或UITextField的UIScrollView问题

时间:2013-04-02 06:26:45

标签: ios cocoa-touch uiscrollview uitextview uikeyboard

我有UIScrollView,其中包含UITextFieldsUITextViews。我已注册UIKeyboardDidChangeFrameNotification。当我点击文本字段或文本视图时,会触发更改框架通知操作,并调整滚动视图的contentOffset,如下所示

- (void)keyboardDidChangeFrame:(NSNotification *)notification
{
    CGRect keyboardEndFrame;
    [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGRect intersection;
    UIView *theFirstResponder = [[UIApplication sharedApplication].keyWindow findFirstResponder];

    if ([theFirstResponder isKindOfClass:[UITextView class]]) {
        if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) {
            keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 416, keyboardEndFrame.size.height, keyboardEndFrame.size.width);
        }
        else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
        {
            keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 0, keyboardEndFrame.size.height, keyboardEndFrame.size.width);
        }
    }
    else
        keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, keyboardEndFrame.origin.x, keyboardEndFrame.size.height, keyboardEndFrame.size.width);

    screenRect = CGRectMake(screenRect.origin.y, screenRect.origin.x, screenRect.size.height, screenRect.size.width);
    if(CGRectEqualToRect(lastKBDRect, keyboardEndFrame)) {
        return;
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    lastKBDRect = keyboardEndFrame;
    if (CGRectIntersectsRect(keyboardEndFrame, screenRect)) {
        // Keyboard is visible

        //Convert Frame of the first responder, in context of the view that needs to be shifted
        UIView *firstResponder = [[UIApplication sharedApplication].keyWindow findFirstResponder];
        CGRect theRect = [firstResponder convertRect:firstResponder.frame toView:[UIApplication sharedApplication].keyWindow];
        theRect = CGRectMake(theRect.origin.y, theRect.origin.x > 768 ? 750 : theRect.origin.x, theRect.size.height, theRect.size.width);

        intersection = CGRectIntersection(keyboardEndFrame, theRect);

        //If intersection is null, then no need to shift anything. Simply return.
        if(CGRectIsNull(intersection)) {
            return;
        }
        //Shift the view so that the first responder view is completely visible, keeping the constraint that the origin of the first responder view is also visible.


        //Remember the current offset, so when we shift the view back, we shift it to the proper position.
        if (!wasContentViewShifted) {
            lastContentOffset = contentScrollView.contentOffset;
            lastContentSize = contentScrollView.contentSize;
            wasContentViewShifted = YES;
        }

        CGFloat offset = theRect.origin.y + theRect.size.height - keyboardEndFrame.origin.y;
        if((theRect.origin.y - offset) < 40) {
            offset += 42;
        }
        [UIView animateWithDuration:0.3f animations:^{
        contentScrollView.contentOffset = CGPointMake(0, contentScrollView.contentOffset.y + offset);
            contentScrollView.contentSize = CGSizeMake(0, lastContentSize.height + (600 - theRect.size.height));
    }];

    } else {
        // Keyboard is hidden. Move the view back only if it was shifted.
        if(wasContentViewShifted) {
            wasContentViewShifted = NO;
            [UIView animateWithDuration:0.3f animations:^{
                contentScrollView.contentOffset = lastContentOffset;
               contentScrollView.contentSize = lastContentSize;
            }];
        }
    }
}

该应用程序仅支持横向。 我面临的问题是

  1. 点击textView会显示键盘,如果键盘隐藏了textview,则会将其滚动到顶部。现在,更改方向(横向左侧为横向右侧)使textView进一步滚动到顶部,因此不可见。
  2. TextView的滚动有时适用于横向左方向,但不适用于横向右方向,反之亦然。这是因为我使用的keyboardEndFrame值。我不应该使用keyboardEndFrame原点值吗?如果没有,会有什么替代方案?
  3. 有时它适用于textField但不适用于textView。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用this,而不是拥有如此复杂的解决方案。 还可以找到一个更简单的解决方案here

另外,建议不要对帧进行硬编码。有关详细信息,请参阅apple documentation