简体   繁体   English

如何以编程方式为UILabels设置约束其中标签计数等于主屏幕高度在Swift中

[英]How to Set constraints for UILabels programmatically Where Label Count is equal to Main screen Height in Swift

I would like to create this Format 我想创建这个格式

label 1 | label 2 | label 3 | label 4
label 5| label 6| label 7|

where labels Count is equal to View Height. 其中标签Count等于View Height。 and want to set constraints also. 并且还想设置约束。 The code below is the Create UILabels: 下面的代码是Create UILabels:

for i in 0...Int(self.view.frame.height)
{
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    label.textAlignment = NSTextAlignment.center
    label.text = "label"
    label.sizeToFit()
    self.view.addSubview(label)
}

It looks you want to stack 4 labels in each row. 它看起来你想在每一行堆叠4个标签。

If you support iOS > 9.0, use UIStackView . 如果您支持iOS> 9.0,请使用UIStackView

Otherwise, you have to calculate the spacing between the labels for each row: 否则,您必须计算每行标签之间的间距:

let spacing = (screenWidth -  (w1 + w2 + w3+ w4))/5

And add the constraints programmatically with .addConstraints . 并使用.addConstraints以编程方式添加约束。

The better way to do it is by using Visual format language but I can't help you out with an answer with context to VFS because I personally don't use it. 更好的方法是使用Visual格式语言但我无法帮助你解决VFS的上下文,因为我个人不使用它。

But if you want to do it with NSLayoutConstraint then it can be done in a very easy way. 但是如果你想用NSLayoutConstraint来做,那么它可以用一种非常简单的方式完成。

I am not writing the exact code but would explain to you how you have to deal this out. 我不是在编写确切的代码,但会向您解释如何解决这个问题。

Note - Don't forget to set translatesAutoresizingMaskIntoConstraints = false and Don't forget to set isActive = true on all the constraints which you want to keep active. 注意 - 不要忘记设置translatesAutoresizingMaskIntoConstraints = false并且不要忘记在要保持活动的所有约束上设置isActive = true。

because if you forget to do it then your code won't work 😅 因为如果你忘记这样做,那么你的代码将不起作用😅

Then align left label1 to the parent view you can either do this by anchors or NSLayoutConstraints I would recommend doing it with Anchor. 然后将左侧label1与父视图对齐,您可以通过锚点或NSLayoutConstraints执行此操作。我建议您使用Anchor执行此操作。

label1.leadingAnchor.constraint(equalTo: parentView.leadingAnchor, constant: 0).isActive = true

Then align other label's leading edge to the trailing edge of the previous one like - 然后将其他标签的前缘与前一个的前缘对齐,如 -

label2.leadingAnchor.constraint(equalTo: label1.leadingAnchor, constant: 0).isActive = true

and finally, align the rightmost label to the trailing edge of parent view like - 最后,将最右边的标签与父视图的后缘对齐,如 -

rightMostLabel.trailingAnchor.constraint(equalTo: parentView.trailingAnchor, constant: 0).isActive = true

Then set the top alignment of the label1 with respect to the parent view 然后设置label1相对于父视图的顶部对齐方式

label1.topAnchor.constraint(equalTo: parentView.topAnchor, constant: 0).isActive = true

Then finally set the width of all the labels equal to any one of the label. 然后最后设置所有标签的宽度等于任何一个标签。 Let's say, we set the width of all the labels equal to label1. 比方说,我们将所有标签的宽度设置为等于label1。 by- 通过-

NSLayoutConstraint(item: label1, attribute: .width, relatedBy: .equal, toItem: label2, attribute: .width, multiplier: 1, constant: 0).isActive = true

This code above was with label1 and label2 you would have to do it again with label2 and label3 and so on. 上面的代码是label1和label2,你必须用label2和label3再做一遍,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM