gpt4 book ai didi

ios - swift 将多个项目与 NSLayoutConstraint 对齐

转载 作者:搜寻专家 更新时间:2023-11-01 06:48:24 26 4
gpt4 key购买 nike

我想在查看大量项目时与 CenterX 对齐。

此代码适用于 1 个项目:

let control_constraint_ItemAlign = NSLayoutConstraint(item: emailField, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)

但我想尝试这样的代码:

let control_constraint_ItemAlign = 
NSLayoutConstraint(item:[emailField,passwordField,loginButton],
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1,
constant: 0)

或者唯一的解决方案是为每个 Item 编写一个约束?

最佳答案

你可以遍历 View :

for view in [emailField,passwordField,loginButton] {
let control_constraint_ItemAlign =
NSLayoutConstraint(item:view,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1,
constant: 0)
self.view.addConstraint(control_constraint_ItemAlign)
}

或者,您可以将适当的选项传递给视觉格式语言

关于ios - swift 将多个项目与 NSLayoutConstraint 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26685186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com