# forEach auto layout Constraints not appearing

**URL:** <https://forums.kodeco.com/t/foreach-auto-layout-constraints-not-appearing/123259>\
**Category:** Swift\
**Created:** [October 23, 2020, 4:33am UTC](https://forums.kodeco.com/t/foreach-auto-layout-constraints-not-appearing/123259 "2020-10-23T04:33:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [October 23, 2020, 4:33am UTC](https://forums.kodeco.com/t/foreach-auto-layout-constraints-not-appearing/123259/1 "2020-10-23T04:33:23Z")

</div>

My swifts code which uses a for loop to display to imageviews. Right now the code builds but nothing appears. I know I have to move the leading anchor constraint over .25 for the second box. But I would think the first box which is box1 would appear but again nothing is appearing and i dont know why. I haave added a photo of my desired output.

```
import UIKit

class ViewController: UIViewController {
    var box1 = UIButton();var box2 = UIButton();var box3 = UIImageView()
    override func viewDidLoad() {
        super.viewDidLoad()
            
        [box1,box2,box3].forEach{
            view.addSubview($0)
            $0.backgroundColor = .red
            $0.translatesAutoresizingMaskIntoConstraints = false
            
            
            $0.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: CGFloat(smith)).isActive = true
            $0.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 15).isActive = true

            $0.heightAnchor.constraint(equalToConstant: 30).isActive = true
            $0.widthAnchor.constraint(equalToConstant: 30).isActive = true
            

        }
    }
    
    
}

```

![6L5B2](https://files.chunter.kodeco.com/original/3X/d/9/d94f2950335d4d66dd34969bfee3de2795e894b7.png)

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [October 23, 2020, 8:50am UTC](https://forums.kodeco.com/t/foreach-auto-layout-constraints-not-appearing/123259/2 "2020-10-23T08:50:05Z")

</div>

You need to have the constraints in relation to the last box, so set the constraints for Box1 and then set them for 1, 2 and 3 relatively.

Alternatively have a lookat snapkit, it is a wonderfully easy way to set up constraints

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [April 7, 2021, 8:33pm UTC](https://forums.kodeco.com/t/foreach-auto-layout-constraints-not-appearing/123259/3 "2021-04-07T20:33:29Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
