Swift 以编程方式向标签添加约束

标签 swift constraints label

我在一个 for index 循环中,并根据每个索引项添加标签。因为我以编程方式添加这些标签,所以我不知道如何将它们限制在设备宽度的最右侧。我在下面解释了我认为约束是如何起作用的。 我的目标 = super View (或包含 View )常量设置为 8 的尾随空间。

let y_align = 340 * index + 70
let x_align = self.deviceWidth - 110
var derp = UILabel(frame: CGRectMake(0, 0, 200, 21))
derp.center = CGPointMake(CGFloat(x_align), CGFloat(y_align))    
derp.textAlignment = NSTextAlignment.Right    
derp.text = json["things"][index]["thing"].string!    
self.some_view_container.addSubview(derp)

//now this is what I understand about constraints    
let xconstraint = NSLayoutConstraint(    
    item: derp, //-- the object that we want to constrain    
    attribute: NSLayoutAttribute.Trailing, //-- the attribute of the object we want to constrain    
    relatedBy: NSLayoutRelation.Equal, //-- how we want to relate THIS object to A DIFF object    
    toItem: some_view_container, //-- this is the different object we want to constrain to    
    attribute: NSLayoutAttribute.Right, //-- the attribute of the different object    
    multiplier: 1, //-- multiplier    
    constant: 8 //-- regular constant    
)

derp.addConstraint(xconstraint)

这就是我想要的人性化

  • item:我想约束derp,它是json的值
  • 属性:我想限制它的尾部空间,以便它一直向右推
  • relatedBy:这里不知道,我不在乎它是否与任何东西相关,因为我只想要父 View 的尾随空格
  • toItem:我想将它推到父 View 的右侧或者它是 some_view_container 的容器
  • attribute:这里不知道,我不关心 View 容器的属性
  • 乘数:我只想将常数乘以 1
  • 常数:我希望尾部空格距离边缘 8

最佳答案

您可以使用此代码作为引用:

import UIKit

class ViewController: UIViewController {
 override func viewDidLoad() {
    super.viewDidLoad()
    addLabels()
 }

 func addLabels(){
    let deviceWidth = UIScreen.mainScreen().bounds.width
    //Here recopilate the constranins for later activation
    var constraints = [NSLayoutConstraint]()
    //Here I'm assuming that there are 5 labels
    for index in 0..<5 {
        //adjust this to spaciate labels
        let y_align = CGFloat(20 * index + 70)
        let x_align = deviceWidth - 110
        var derp = UILabel(frame: CGRectMake(0, 0, 200, 21))
        derp.center = CGPointMake(CGFloat(x_align), CGFloat(y_align))
        derp.textAlignment = NSTextAlignment.Right
        derp.text = "things \(index)"

        let xconstraint = NSLayoutConstraint(
            item: derp, //-- the object that we want to constrain
            attribute: NSLayoutAttribute.Trailing, //-- the attribute of the object we want to constrain
            relatedBy: NSLayoutRelation.Equal, //-- how we want to relate THIS object to A DIFF object
            toItem: self.view, //-- this is the different object we want to constrain to
            attribute: NSLayoutAttribute.Right, //-- the attribute of the different object
            multiplier: 1, //-- multiplier
            constant: -8 //-- (Take a look here, its a minus)
        )
        let yconstraint = NSLayoutConstraint(
            item: derp, //-- the object that we want to constrain
            attribute: NSLayoutAttribute.Top, //-- the attribute of the object we want to constrain
            relatedBy: NSLayoutRelation.Equal, //-- how we want to relate THIS object to A DIFF object
            toItem: self.view, //-- this is the different object we want to constrain to
            attribute: NSLayoutAttribute.TopMargin, //-- the attribute of the different object
            multiplier: 1, //-- multiplier
            constant: y_align //-- regular constant
        )
        //recopilate constraints created here
        constraints.append(xconstraint)
        constraints.append(yconstraint)
        derp.setTranslatesAutoresizingMaskIntoConstraints(false)
        //add them to the desired control
        self.view.addSubview(derp)
    }
    //This will tell iOS to use your constraint
    NSLayoutConstraint.activateConstraints(constraints)
 }
}

关于Swift 以编程方式向标签添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147088/

相关文章:

vb.net - 如何将长行换行到标签控件中?

ios - 从 Swift 中的非静态方法访问静态变量

MySQL 将约束表示为计算列?

c - Z3 无符号变量 - 简化

ios - 以编程方式删除约束的效果 xcode

c# - 如何从列表框项目值设置标签文本? C#

ios - 以圆形模式向外移动 SpriteNode

ios - 如何在 UITableView swift 中更改行长度

Swift 3 如何在 UITableViewRowAction 中引用行索引

javascript - 长刻度标签在 plotly.js 图表中被 chop