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

时间:2015-06-30 19:42:43

标签: swift constraints labels

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

[Route("{id}")]
public QuoteModel Get(int id)
{
    return quoteService.GetQuoteById(id).Map<QuoteModel>();
}

这就是我想要的人类意义

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

2 个答案:

答案 0 :(得分: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)
 }
}

答案 1 :(得分:2)

适用于swift 3.0

func addLabels(){
    let deviceWidth = UIScreen.main.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: CGRect(x: 0, y: 0, width: 200, height: 100))
        derp.center = CGPoint(x:CGFloat(x_align), y: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.translatesAutoresizingMaskIntoConstraints = false
        //add them to the desired control
        self.view.addSubview(derp)
    }
    //This will tell iOS to use your constraint
    NSLayoutConstraint.activate(constraints)
}