滚动视图出现问题,滚动更多

时间:2011-02-24 10:27:16

标签: iphone uiscrollview scroll uitextfield

我有一个scrollView,我想在选择textField时自动滚动它 (我在这里填写表格)
我正在使用以下方法滚动它

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{   
[scrollView setContentOffset:CGPointMake(0,50*(textField.tag-1))];
}

现在的问题是我有超过10个文本字段,当我到达第七个textField时,scrollView滚动更多。 我还尝试打印CGPointMake()的值...并显示正确的值..但是滚动条超出了预期的范围。 看下面的图片

以下2张图片显示了对textFields标签< 7

的控制

enter image description here enter image description here

但是当控制到达第7个textField时,它会滚动更多

enter image description here

之后它超越了界限。

只有当我从一个textFields移动到另一个textFields而不按下返回按钮(我的意思是resignFirstResponder)时才会出现此问题。但是当我按下该返回按钮然后转到下一个字段时,一切正常。

任何人都可以建议问题出在哪里..?

3 个答案:

答案 0 :(得分:1)

它应该保持屏幕上正在编辑的文本字段 - 即自动滚动 - 所以你不需要做任何事情?

但是,它可能正在尝试编辑键盘后面的文本字段 - 因此更好的解决方案是:

//Get notifications of the keyboard opening and closing
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

- (void)keyboardWillShow:(NSNotification *)notification {
    //Get the keyboard height
    int h = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size.height;
    //Change the inset of the scroll view and scroll bars
    scrollView.contentInset = scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, h, 0);
}
- (void)keyboardWillHide:(NSNotification *)notification {
    scrollView.contentInset = scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}

答案 1 :(得分:1)

它不会滚动,因为它不包含足够的内容。当内容的下边缘到达框架的底端时,它将停止。使用像

这样的东西
CGSize size = scrollView.contentSize;
scrollView.contentSize = CGSizeMake (size.width, size.height + ADDITIONAL_HEIGHT);

设置滚动视图时,或者在-viewDidLoad:方法中,如果它是从XIB加载的。

答案 2 :(得分:0)

尝试this.declare变量

     CGPoint svos;
<。>在.h文件中并执行此操作。

       - (void)textFieldDidBeginEditing:(UITextField *)textField {
svos = scr.contentOffset;//scr is my scroll view
CGPoint pt;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:scr];
pt = rc.origin;
pt.x = 0;
pt.y -= 60;
[scr setContentOffset:pt animated:YES];   
//NSLog(@"%f",pt.y);
     }