gpt4 book ai didi

ios - 在 VFL 中创建垫片

转载 作者:行者123 更新时间:2023-11-29 02:22:13 26 4
gpt4 key购买 nike

AutoLayoutbyExample via Apple Developer Documentation

有人有示例说明如何创建间隔 View 以在 VFL 中设置标签之间的间距相等吗?

labelsDictionary = [ "label1": label1,
"label2": label2,
"label3": label3,
"label4": label4 ]

let metrics = ["edgeSpacing": 20, "spacingBetween": 5, "labelWidth": 50]
let constraints = "H:|-(==edgeSpacing)-[label1(==labelWidth@999)]-[label2(==label1)]-[label3(==label1)]-[label3(==label1)]-[label4(==label1)]-(==edgeSpacing)-|"
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( format : constraints,
options : .allZeros,
metrics : metrics,
views : labelsDictionary))

我只是想知道如何在 Swift 中创建间隔符以确保标签之间的宽度始终相等但优先级最低。

enter image description here

最佳答案

您可以添加 3 个间隔 View ,它们与 4 个标签混合。

标签1-间隔1-标签2-间隔2-标签3-间隔3-标签4

假设您在一个containerView 中拥有所有7 个 View 。由于标签宽度可能会根据其文本而变化,因此它们之间的间距最好相等。

self.containerView.removeConstraints(containerView.constraints())

let views = ["label1": label1,
"label2": label2,
"label3": label3,
"label4": label4,
"spacer1": spacer1,
"spacer2": spacer2,
"spacer3": spacer3
]
let metrics = ["edgeSpacing": 20, "spacingBetween": 5]

//make spacers have equal width, and greater than 5, make all views center align vertically
var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-edgeSpacing-[label1]-[spacer1(>=spacingBetween)]-[label2]-[spacer2(==spacer1)]-[label3]-[spacer3(==spacer1)]-[label4]-edgeSpacing-|", options: .AlignAllCenterY, metrics: metrics, views: views)
view.addConstraints(constH)

//configure label1 vertical alignment
let constV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[label1]-edgeSpacing-|", options: .AlignAllCenterY, metrics: metrics, views: views)
view.addConstraints(constV)

//make spacers have equal height
let constVForSpacer1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[spacer1(==spacer2)]-edgeSpacing-|", options: .allZeros, metrics: metrics, views: views)
view.addConstraints(constVForSpacer1)

let constVForSpacer2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-edgeSpacing-[spacer2(==spacer3)]-edgeSpacing-|", options: .allZeros, metrics: metrics, views: views)
view.addConstraints(constVForSpacer2)

关于ios - 在 VFL 中创建垫片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017842/

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