【问题标题】:How to move down many UITextFields if a new UITextField is added如果添加了新的 UITextField,如何向下移动许多 UITextField
【发布时间】:2013-11-11 13:10:44
【问题描述】:

我有一个 ViewController,许多 UITextFields(一个在另一个下面)和一个按钮在第一个 UITextField 的右侧。

当按下按钮时,我希望在此下方显示另一个 UITextField(每次我按下按钮时,都会在另一个下创建一个文本字段)并向下移动其他一些像素。

我该怎么做?我想我必须用 InterfaceBuilder 制作一个新的 UIView,其中的文本字段会向下移动一些像素,但我不能这样做。

【问题讨论】:

    标签: ios uiview uitextfield


    【解决方案1】:

    使用 UITableViewController 怎么样?基本上,您可以将第一个 UITextField(在 UITableViewCell 内)和第二部分中的其余字段设置为第一部分。然后,每次按下按钮时,您都会在第一部分插入一个新单元格。

    管理字段的插入和删除很容易,无需手动处理视图的移动(它们由 UITableView 自动处理),您所要做的就是更新数据源(单元格数) 并重新加载/插入单元格(reloadData 或 insertRowsAtIndexPaths:withRowAnimation:)。

    【讨论】:

    • 我可以使用界面生成器将文本字段添加到单元格中,还是必须以编程方式创建它们?
    • 如果您在 Storyboard 中使用 UITableViewController,您可以在内部创建带有 UITextField 的 UITableViewCell 原型(直接来自 Interface Builder)。
    【解决方案2】:

    如果您愿意,我建议一个非常简单的解决方案,即使用变量代替 CGRectMake 坐标中的 y 坐标

    更多细节:

    您必须以编程方式创建所有 UITextField,并且每当您分配 initwithFrame 时,任何 UITextField 都使用如下变量:

    int y = 10; // By default value and make it a property and define in viewDidLoad or wherever
    
    UITextField *txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 20 +y, 100, 50)];
            // set TextFields other values
    [self addSubview:txtField1];
    
    UITextField *txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 80+y, 100, 50)];
            // set TextFields other values
    [self addSubview:txtField1];
    

    现在,每当您单击添加 UITextField 按钮时,将 y 的值增加您想要的额外空间,例如:

    -(void)onTapAddTextFieldButton{
        y= y + 40;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多