Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 创建一个循环以在uiviewcontroller上分隔两个不同的对象_Loops_For Loop_Foreach_Nslayoutconstraint_Cgfloat - Fatal编程技术网

Loops 创建一个循环以在uiviewcontroller上分隔两个不同的对象

Loops 创建一个循环以在uiviewcontroller上分隔两个不同的对象,loops,for-loop,foreach,nslayoutconstraint,cgfloat,Loops,For Loop,Foreach,Nslayoutconstraint,Cgfloat,我下面的代码正是我想要的,问题是它只是在uibtton中实现了这一点。我想用2个UIExtFields和2个UILabel做同样的事情。所以textfield到uilabel到textfield到uilabel。我想你只需要改变“按钮在”,但我不知道用什么来改变它。我希望对象之间的间距为40,如下所示 func setConstraints() { var yPosition: CGFloat = 0 [undoButton, clearButton, color].forEach { but

我下面的代码正是我想要的,问题是它只是在uibtton中实现了这一点。我想用2个UIExtFields和2个UILabel做同样的事情。所以textfield到uilabel到textfield到uilabel。我想你只需要改变“按钮在”,但我不知道用什么来改变它。我希望对象之间的间距为40,如下所示

func setConstraints() {
var yPosition: CGFloat = 0

[undoButton, clearButton, color].forEach { button in
    NSLayoutConstraint.activate([
        button.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :25),
        button.topAnchor.constraint(equalTo: view.centerYAnchor, constant : yPosition),
        button.widthAnchor.constraint(equalToConstant: CGFloat(widthBox)),
        button.heightAnchor.constraint(equalToConstant: 20)
    ])
    yPosition += 40
}

}

只需将文本字段和标签按所需顺序排列在一个数组中,然后用它替换
[undoButton,clearButton,color]
部分。从技术上讲,您不需要更改部分中的
按钮,因为它只是一个变量名

[textField1, label1, textField2, label2].forEach { view in
    NSLayoutConstraint.activate([
        view.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :25),
        view.topAnchor.constraint(equalTo: view.centerYAnchor, constant : yPosition),
        view.widthAnchor.constraint(equalToConstant: CGFloat(widthBox)),
        view.heightAnchor.constraint(equalToConstant: 20)
    ])
    yPosition += 40
}
请注意,这可能会让你的生活更轻松。我建议你看看如何使用它