2

我需要动态地将文本字段添加到滚动视图。我能够做到,添加后我每次都会计算和更新滚动视图的内容大小。

更新内容大小后,我可以在屏幕上查看它,一旦我编辑文本字段(键盘消失),我将内容大小重置为原始值。我在这里错过了什么吗?滚动视图是通过 xib 创建的。

4

3 回答 3

0

您可以跟踪最后一个 textFiled 的 y 位置。

现在在将所有文本字段添加到 ScrollView 后使用下面的行。

[scrollView setContentSize:CGSizeMake(320, yPos)];

对于键盘问题用户,请使用以下代码。

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

[textField resignFirstResponder];
[scrollView setContentSize:CGSizeMake(320, yPos)];
return YES;
}

希望这会帮助你。

一切顺利 !!!

于 2013-03-14T11:41:09.803 回答
0
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
    {
        self.scrollView.frame = CGRectMake(0, 0, 320, 240);// set height of self.scrollView.frame, as you need.
        return YES;
    }

    - (BOOL)textFieldShouldReturn:(UITextField *)textField 
   {

        [self.txtFieldName resignFirstResponder];
        .
        ,
        .

        self.scrollView.frame = CGRectMake(0, 0, 320, 460); // set height of self.scrollView.frame, as you need.
        [self.scrollView setContentSize:CGSizeMake(320, 465)]; // set height of self.scrollView.ContentSize, as you need.

        return YES;
    }
于 2013-03-14T11:50:58.327 回答
0

viewDidLoad方法中,

y = 500; // As you wish

scrMain.delegate = self;
scrMain.contentSize = CGSizeMake(320, y);

然后,对于文本字段输入

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    scrMain.contentSize = CGSizeMake(320, y + 50);
}

返回键后,

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    scrMain.contentSize = CGSizeMake(320, y);
    return YES;
 }
于 2013-03-14T11:50:58.743 回答