0

我有几个UITextFields在一个UIScrollView。当我编辑其中一个并弹出键盘时,该字段被键盘覆盖。

我想向上滚动视图,我认为这是由 iOS 自动完成的,但似乎并非如此。

我目前正在使用这种方法来滚动视图,但效果不是很好。

- (void)scrollToView:(UIView *)view
{
    CGRect viewFrame = [[edit scrollView] convertRect:[view frame] fromView:[view superview]];

    CGRect finalFrame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width, (viewFrame.size.height + (inputAccessory.frame.size.height) + 4.0));

    [[edit scrollView] scrollRectToVisible:finalFrame animated:YES];
}

谢谢

4

5 回答 5

2

就我而言,我所做的是在 的情况下减小 的大小scrollViewEditing Did Begin如下UITextField所示:

- (IBAction)didEnterInTextField:(id)sender
{
    [sender becomeFirstResponder];
    // Resize the scroll view to reduce the keyboard height
    CGRect scrollViewFrame = self.scrollView.frame;
    if (scrollViewFrame.size.height > 300) {
        scrollViewFrame.size.height -= 216;
        self.scrollView.frame = scrollViewFrame;
    }

    // Scroll the view to see the text field
    UITextField *selectedTextField = (UITextField *)sender;
    float yPosition = selectedTextField.frame.origin.y - 60;
    float bottomPositon = self.scrollView.contentSize.height - self.scrollView.bounds.size.height;
    if (yPosition > bottomPositon) {
        yPosition = bottomPositon;
    }
    [self.scrollView setContentOffset:CGPointMake(0, yPosition) animated:YES];
}
于 2013-01-07T12:13:03.800 回答
1

这是同样的好教程

http://www.iphonesampleapps.com/2009/12/adjust-uitextfield-hidden-behind-keyboard-with-uiscrollview/

希望对你有帮助

于 2013-01-07T12:12:37.320 回答
1

试试这个示例代码TPKeyboardAvoiding

它易于实施。只需拖放自定义类并将自定义类从 UIScrollview 更改TPKeyboardAvoidingScrollView为 xib

于 2013-01-07T12:25:53.363 回答
0

//注册键盘通知

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

在这两种方法中设置滚动视图的框架..当键盘显示或隐藏时。

或者设置滚动视图的scrollRectToVisible

于 2013-01-07T12:12:05.667 回答
0
- (void)scrollViewToCenterOfScreen:(UIView *)theView
{
    CGFloat viewCenterY = theView.center.y;
    CGFloat availableHeight;
    CGFloat y;

    if(!isReturned)
    {
        if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
        {
            availableHeight = 1080;
        }
        else
        {
            availableHeight = 220;
        }
    }
    else
    {
        if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
        {
            availableHeight = 1400;
        }
        else
        {
            availableHeight = 530;
        }

    }

    y = viewCenterY - availableHeight / 2.0;
    if (y < 0) {
        y = 0;
    }
    [sclView setContentOffset:CGPointMake(0, y) animated:YES];
}

在文本字段或文本视图中调用此方法开始编辑。它将起作用。

于 2013-01-07T12:30:48.957 回答