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

标签 ios uiscrollview

我已将动态创建的文本字段放置在 ScrollView 中。我需要为 ScrollView 设置 contentOffset。在文本字段中应该开始编辑我已经给出了 ScrollView 。如果我遍历“完成”按钮,效果很好。但在这之间,如果我点击任何文本字段,该文本字段就会上升,并且我看不到该文本字段。它显示下一个字段偏移量,但光标位于正确的文本字段中。我的代码是

  -(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;

        }
    }

}

请帮我解决这个问题。

最佳答案

首先为每个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;
}

关于ios - 如何在动态创建的文本字段中设置 ScrollView 的内容偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12276055/

相关文章:

ios - SKStoreReviewController 和深色模式

按下取消按钮时,IOS 7 UISearchBar 搜索图标不会动画到中心

ios - iOS 应用程序的典型目录结构

ios - UITableView 与 UIRefreshControl 的拖动距离

objective-c - XML 解析类在重新运行时泄漏字符串

iphone - 如何赋予UIImage负色效果

iphone - UIScrollView block 运行循环?

ios - Storyboard生成的 UIScrollView 不滚动

ios - 将 UIView 从一个 UIScrollView 拖到另一个 UIScrollView

ios - UIScrollView 在横向模式下不滚动 - 附上测试代码和屏幕截图