如何在动态创建的文本字段中设置scrollview的内容偏移量?

时间:2012-09-05 06:57:47

标签: ios uiscrollview

我在scrollview中放置了动态创建的文本字段。我需要为scrollview设置contentOffset。在textfield shouldbegin Editing我已经给出了scrollview。如果我遍历完成按钮,它工作正常。但是,如果我点击任何文本字段,文本字段上升,我无法看到文本字段。它显示nextfield偏移但光标位于正确的文本字段中。我的代码是

  -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

if(enteredHealthyHeart)
{
    for(int i=0;i<[healthyHeart_TxtFldAry count];i++)
    {
        if(textField == [healthyHeart_TxtFldAry objectAtIndex:i])
        {
            getHHTag=textField.tag;
            NSLog(@"getHHTag %i",getHHTag);
            UITextField *tempTxtFld=[healthyHeart_TxtFldAry objectAtIndex:getHHTag];

            if(i<([healthyHeart_TxtFldAry count]-1))
            {
                int j =tempTxtFld.frame.origin.y;

                healthyHeartScrollView.contentOffset=CGPointMake(0 , j);

                NSLog(@"j>>>>>%i",j);
            }
            if(i==([healthyHeart_TxtFldAry count]-1))
            {
                healthyHeartScrollView.contentOffset=CGPointMake(0 ,tempTxtFld.frame.origin.y);

            }

            [tempTxtFld resignFirstResponder];

            return YES;

        }
    }

}

}

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

首先将标签添加到每个UITextField;

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //Take reference of all UITextField u added let say u have 3;
    UITextField *txt = (UItextField *)[self.view viewWithTag:99];
    UITextField *txt1 = (UItextField *)[self.view viewWithTag:199];
    UITextField *txt2 = (UItextField *)[self.view viewWithTag:299];

    if(textField == txt)
    {
       [txt1 resignFirstResponder];
       [txt2 resignFirstResponder];
       //required scroll view offset here
    }
    if(textField == txt1)
    {
       [txt resignFirstResponder];
       [txt2 resignFirstResponder];
       //required scroll view offset here
    }
    if(textField == txt2)
    {
       [txt resignFirstResponder];
       [txt1 resignFirstResponder];
       //required scroll view offset here
    }
    return YES;
}

答案 1 :(得分:0)

scrollview.contentOffset是内容视图从orgin滚动视图偏移的点。在您的代码中

healthyHeartScrollView.contentOffset=CGPointMake(0 ,tempTxtFld.frame.origin.y);

tempTxtFld超出了healthyHeartScrollView的内容视图。这使得tempTxtFld.frame.orgin.y相对于healthyHeartScrollView的内容视图为负。 所以你看不到它。

PS。我真的不知道你想做什么。-_-