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

后端 未结 2 2286
鱼传尺愫
鱼传尺愫 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: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.

提交回复
热议问题