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

Bra*_*eon 1 constraints labels swift

我处于for索引循环中,并基于每个索引项添加标签。因为我是以编程方式添加这些标签的,所以我不知道如何将它们限制在设备宽度的最右边。我在下面解释我认为约束的工作原理。我的目标=将超级视图(或包含视图)的尾随空间常量设置为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)
Run Code Online (Sandbox Code Playgroud)

这就是我想要的人类

  • 项目:我想约束DERP,这是JSON的值
  • 属性:我想限制它的尾随空间,以便将其一直推到右边
  • relatedBy:在这里不知道,我不在乎它是否与任何东西有关,因为我只想要超级视图的尾部空间
  • toItem:我想将其推到超级视图的右侧,或者它是some_view_container的容器
  • 属性:这里不知道,我不在乎视图容器的属性
  • 乘数:我只想要常数的数目,所以乘以1
  • 常量:我希望尾随距离边缘8

Hug*_*nso 5

您可以使用以下代码作为参考:

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)
 }
}
Run Code Online (Sandbox Code Playgroud)