How to add constraints programmatically using Swift

前端 未结 17 1170
逝去的感伤
逝去的感伤 2020-11-21 23:07

I\'m trying to figure this out since last week without going any step further. Ok, so I need to apply some constraints programmatically

17条回答
  •  孤独总比滥情好
    2020-11-21 23:33

    It helps me to learn visually, so this is a supplemental answer.

    Boilerplate code

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let myView = UIView()
        myView.backgroundColor = UIColor.blue
        myView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(myView)
    
        // Add constraints code here
        // ...
    }
    

    Each of the following examples are independent of the others.

    Pin left edge

    myView.leading = leadingMargin + 20

    Method 1: Anchor Style

    let margins = view.layoutMarginsGuide
    myView.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 20).isActive = true
    
    • In addition to leadingAnchor, there is also trailingAnchor, topAnchor, and bottomAnchor.

    Method 2: NSLayoutConstraint Style

    NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.leadingMargin, multiplier: 1.0, constant: 20.0).isActive = true
    
    • In addition to .leading there is also .trailing, .top, and .bottom.
    • In addition to .leadingMargin there is also .trailingMargin, .topMargin, and .bottomMargin.

    Set Width and Height

    width = 200
    height = 100

    Method 1: Anchor Style

    myView.widthAnchor.constraint(equalToConstant: 200).isActive = true
    myView.heightAnchor.constraint(equalToConstant: 100).isActive = true
    

    Method 2: NSLayoutConstraint Style

    NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 200).isActive = true
    NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100).isActive = true
    

    Center in container

    myView.centerX = centerX
    myView.centerY = centerY

    Method 1: Anchor Style

    myView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    myView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    

    Method 2: NSLayoutConstraint Style

    NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0).isActive = true
    

    Notes

    • Anchor style is the preferred method over NSLayoutConstraint Style, however it is only available from iOS 9, so if you are supporting iOS 8 then you should still use NSLayoutConstraint Style.
    • The examples above showed just the one or two constraints that were being focused on. However, in order to properly place myView in my test project I needed to have four constraints.

    Further Reading

    • Programmatically Creating Constraints documentation

提交回复
热议问题