Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 constraints through auto layout.

my code look like below:

  var countNoOfViews:Int = 3   
 @IBOutlet var viewForRow1: UIView!

override func viewDidLoad() {
        super.viewDidLoad()
 self.specialButtonViewLeft()
}

func specialButtonViewLeft(){

    for i in 0..<countNoOfViews{
        var customView:UIView!
        customView = UIView(frame: CGRect.zero)
        customView.translatesAutoresizingMaskIntoConstraints = false
        viewForRow1.addSubview(customView)

        let widthConstraint = NSLayoutConstraint(item: customView, attribute: .width, relatedBy: .equal,toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 20.0)

        let topConstraint = NSLayoutConstraint(item: customView, attribute: .top, relatedBy: .equal,toItem: self.viewForRow1, attribute: .top, multiplier: 1.0, constant: 0)

        let bottomConstraint = NSLayoutConstraint(item: customView, attribute: .bottom, relatedBy: .equal,toItem: self.viewForRow1, attribute: .bottom, multiplier: 1.0, constant: 0)

        let leadingConstraint = NSLayoutConstraint(item: customView, attribute: .leading, relatedBy: .equal, toItem: self.viewForRow1, attribute: .leading, multiplier: 1, constant: customLeadingSpaceLeft)

        customLeadingSpaceLeft = customLeadingSpaceLeft + customViewWidth

        arrayLeftBtnConstraints.append(widthConstraint)

        if i == 0{
            customView.backgroundColor = UIColor.red
        }else if i == 1{
            customView.backgroundColor = UIColor.green
        }else if i == 2{
            leftViewVal = customLeadingSpaceLeft
            customView.backgroundColor = UIColor.black
        }
        customView.alpha = 0.50
        viewForRow1.addConstraints([widthConstraint, leadingConstraint,topConstraint,bottomConstraint])
    }
}

I want to add equal width constraint programmatically.

like image 997
Chirag Patel Avatar asked Dec 05 '16 09:12

Chirag Patel


People also ask

How do I give a constraint to button programmatically in Swift?

Add the button in the view, give it constraints and as you are using constraints, you can skip the button. frame and add widthAnchor and heightAnchor . At last activate them and keep translatesAutoresizingMaskIntoConstraints as false . Also, it will be better if you can add proper names.

How do you add equal width constraints?

To add equal width constraint, you will need to select both views by command clicking on both of them. Then click on the pin menu and the previously grayed out Equal Width checkbox is now available. Click on that and click Add 1 Constraint.

What is Autolayout in IOS Swift?

Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.


Video Answer


2 Answers

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.

like image 118
vsilux Avatar answered Sep 23 '22 04:09

vsilux


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.

like image 20
Bista Avatar answered Sep 25 '22 04:09

Bista