This is how I am showing my image views inside the scroll view through for loop:

```
func addPageController() {
        let imgArray = ["AquariumBG", "PaludariumBG", "TerrariumBG"]

        let scrollContentViewWidthAnchor = scrollContentView.widthAnchor.constraint(equalTo: headerScrollView.widthAnchor, constant:0)
        scrollContentViewWidthAnchor.isActive = true

        for (index, item) in imgArray.enumerated() {
            let imgView = UIImageView()
            imgView.contentMode = .scaleAspectFill
            imgView.image = UIImage(named: item)
            let xPos = CGFloat(index) * UIScreen.main.bounds.size.width

            scrollContentView.addSubview(imgView)
            scrollContentView.translatesAutoresizingMaskIntoConstraints = false
            scrollContentView.topAnchor.constraint(equalTo: headerScrollView.topAnchor, constant: 0).isActive = true
            scrollContentView.bottomAnchor.constraint(equalTo: headerScrollView.bottomAnchor, constant: 0).isActive = true
            scrollContentView.leadingAnchor.constraint(equalTo: headerScrollView.leadingAnchor, constant: 0).isActive = true
            scrollContentViewWidthAnchor.constant = xPos

            scrollContentView.setNeedsLayout()
            scrollContentView.layoutIfNeeded()

            imgView.translatesAutoresizingMaskIntoConstraints = false
            imgView.topAnchor.constraint(equalTo: scrollContentView.topAnchor, constant: 0).isActive = true
            imgView.bottomAnchor.constraint(equalTo: scrollContentView.bottomAnchor, constant: 0).isActive = true
            imgView.widthAnchor.constraint(equalToConstant: headerScrollView.frame.size.width).isActive = true
            imgView.leadingAnchor.constraint(equalTo: scrollContentView.leadingAnchor, constant: xPos).isActive = true
            imgView.widthAnchor.constraint(equalToConstant: headerScrollView.frame.size.width).isActive = true

            imgView.setNeedsLayout()
            imgView.layoutIfNeeded()
        }
    }

```

And this is the output:

[![enter image description here](https://i.stack.imgur.com/SAWSL.png)](https://i.stack.imgur.com/SAWSL.png)

It is showing perfectly. Now If I swipe the header down to the screen (Cause it is stretchy header) the second image also shows with the first image like this:

[![enter image description here](https://i.stack.imgur.com/ULyrl.png)](https://i.stack.imgur.com/ULyrl.png)

The reason for this is because the image views have no `trailingAnchor.constraint`. So my question is, is it possible to set trailing anchor with the image views inside a loop, when the next image view hasn't created yet? If so how? Any suggestions would be highly appreciated. Thanks in advance.

My Storyboard: 

[![enter image description here](https://i.stack.imgur.com/t7R7J.png)](https://i.stack.imgur.com/t7R7J.png)

**[SAMPLE PROJECT LINK](https://www.dropbox.com/s/hgriju6j5cxnz8p/TableViewWithStretchyHeaderEffect.zip?dl=0)**