Brandon Sturgeon Brandon Sturgeon - 1 month ago 18
Swift Question

Swift programmatically adding constraints to labels

I am in a for index loop and adding labels based off each index item. Because I am programmatically adding these labels I don't know how to constrain them to the far right of the device width. I explain below how I think the constraints work.
My goal = trailing space to superview (or containing view) constant set to 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)


this is what I want in human terms


  • item: I want to constrain the derp which is the value of the json

  • attribute: I want to constrain the trailing space of it so it pushes it all the way to the right

  • relatedBy: no idea here, I don't care if it is related to anything, because I just want the trailing space to the superview

  • toItem: I want to push it to the right of the superview OR it's container which is some_view_container

  • attribute: no idea here, I don't care about the attribute of the view container

  • multiplier: I just want the number of the constant so multiply by 1

  • constant: I want the trailing space to be 8 away from the edge


Answer

You can use this code as reference:

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)
 }
}
Comments