我刚接触iPhone,我的应用程序中有10个文本字段,这些文本字段处于滚动视图中。
我需要的是,当用户触摸文本框时,scrollview应该以这种方式滚动,这样文本框就不应位于键盘后面。

帮我。

感谢您对我的帮助。

最佳答案

覆盖这样的文本字段委托方法

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self scrollViewToCenterOfScreen:textField];
}


//此方法将当前选定的文本字段移动到可见区域

-(void)scrollViewToCenterOfScreen:(UIView *)theView
{
    CGFloat viewCenterY = theView.center.y;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

    CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard  

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


当您想关闭键盘时,请覆盖此委托

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
        [textField resignFirstResponder];
        [self.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    return YES;
}

关于iphone - scrollView iPhone内的文本字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12155787/

10-11 22:28
查看更多