UIScrollView中的几个UITextField,由键盘覆盖

时间:2013-01-07 12:06:23

标签: objective-c ios uiscrollview uitextfield

我在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];
}

感谢

5 个答案:

答案 0 :(得分:2)

在我的情况下,我所做的是减少scrollView Editing 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];
}

答案 1 :(得分:1)

答案 2 :(得分:1)

试试此示例代码TPKeyboardAvoiding

易于实施。只需拖动和drog自定义类,并将自定义类从UIScrollview更改为xib中的TPKeyboardAvoidingScrollView

答案 3 :(得分:0)

//注册键盘通知

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

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

或设置滚动视图的 scrollRectToVisible

答案 4 :(得分: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];
}

在textfield或textview中调用此方法开始编辑。它将起作用。