How to create Auto layout equal width constraint programmatically in ios swift?

后端 未结 2 2287
鱼传尺愫
鱼传尺愫 2021-02-19 03:28

How to give programmatically constraints equal width and equal height with multiple views.I check google but not perfect answer for programmatically equal width and height const

相关标签:
2条回答
  • 2021-02-19 03:53

    Try this:

    let widthConstraint = NSLayoutConstraint(item: customView, attribute: .width, relatedBy: .equal, toItem: viewForRow1, attribute: .width, multiplier: 0.25, constant: 0.0)
    

    multiplier: 0.25, denotes that customView's width will be 1/4th of the parent view, viewForRow1.

    0 讨论(0)
  • 2021-02-19 03:58

    You have three possible ways to do that:

    1) By using anchors:

    view.widthAnchor.constraint(equalTo: otherView.widthAnchor, multiplier: 1.0).isActive = true

    2) Visual format:

    simple example - "H:|[otherView]-[view(==otherView)]|"

    3) "Old school" constraints:

    NSLayoutConstraint(item: #view, attribute: .width, relatedBy: .equal, toItem: #otherView, attribute: .width, multiplier: 1.0, constant: 0.0).isActive = true

    hope it will help somebody.

    0 讨论(0)
提交回复
热议问题