Quick explanation.
I have a UIScrollView, that's a sub-view of "self.view". My UIScrollview, has a subview called "contentView"

All my other objects are subviews of contentView or it's sub-views

 I got everything looking correctly, with the exception of 2 views.
 
Once I add these views in my constraints. The entire contentView shifts to the left, and breaks everything.

Here's an image of the screen with the troublesome view called (howManyIconView), it's the blue 
UIView, with a white slack icon in the middle. (bottom left)
 
To make it clear to everyone that it's the contentView that's getting messed up, I gave the contentView a red background.

/images/forum/upload/2015-03-21/cf602881343c863f15e70274388fb072.png

Here is the same view, without adding the howManyIconView to my constraint.

/images/forum/upload/2015-03-21/c3318ae181c79e1fe8578c4402c5bc49.png

To the actual code related to the problem, starting from the top.


contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(contentView)

howManyContentView.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.addSubview(howManyContentView)

howManyIconView.setTranslatesAutoresizingMaskIntoConstraints(false)
howManyIconView.backgroundColor = UIColor.formulaBlueColor()
howManyContentView.addSubview(howManyIconView)

howManyIcon.setTranslatesAutoresizingMaskIntoConstraints(false)
howManyIcon.textColor = UIColor.formulaWhiteColor()
howManyIcon.font = UIFont(name: "fontAwesome", size: 20)
howManyIcon.text = "?"
howManyIconView.addSubview(howManyIcon)

And here's the constraints:


var viewsDictionary = ["contentView":contentView,"howManyContentView":howManyContentView,
            "howManyLabel":howManyLabel,
            "howManyInputField":howManyInputField,
            "howManyIconView":howManyIconView,
            "howManyIcon":howManyIcon]

    let metricsDictionary = ["topBarHeight":6,"numbersViewRowHeight":49,"numbersViewSeperatorHeight":1,"inputFieldHeight":75,"contentWidth":self.view.bounds.width,"grapViewHeight":175,"inputFieldWidth":100,"iconViewWidth":50]

            scrollView.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "H:[contentView(contentWidth)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary))

        howManyContentView.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "V:[howManyIconView(inputFieldHeight)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary))
        howManyContentView.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "V:[howManyInputField(inputFieldHeight)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary))

contentView.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "H:|[howManyContentView]|", options: nil, metrics: nil, views: viewsDictionary))

        howManyContentView.addConstraint(NSLayoutConstraint(item:howManyLabel, attribute:NSLayoutAttribute.CenterY, relatedBy:NSLayoutRelation.Equal, toItem:howManyContentView, attribute:NSLayoutAttribute.CenterY, multiplier:1.0, constant:0))
        howManyContentView.addConstraint(NSLayoutConstraint(item:howManyInputField, attribute:NSLayoutAttribute.CenterY, relatedBy:NSLayoutRelation.Equal, toItem:howManyContentView, attribute:NSLayoutAttribute.CenterY, multiplier:1.0, constant:0))
        howManyContentView.addConstraint(NSLayoutConstraint(item:howManyIconView, attribute:NSLayoutAttribute.CenterY, relatedBy:NSLayoutRelation.Equal, toItem:howManyContentView, attribute:NSLayoutAttribute.CenterY, multiplier:1.0, constant:0))
        howManyIconView.addConstraint(NSLayoutConstraint(item:howManyIcon, attribute:NSLayoutAttribute.CenterY, relatedBy:NSLayoutRelation.Equal, toItem:howManyIconView, attribute:NSLayoutAttribute.CenterY, multiplier:1.0, constant:0))
        howManyIconView.addConstraint(NSLayoutConstraint(item:howManyIcon, attribute:NSLayoutAttribute.CenterX, relatedBy:NSLayoutRelation.Equal, toItem:howManyIconView, attribute:NSLayoutAttribute.CenterX, multiplier:1.0, constant:0))


        howManyContentView.addConstraints(
    NSLayoutConstraint.constraintsWithVisualFormat(
        "H:|-32-[howManyLabel]", options: nil, metrics: metricsDictionary, views: viewsDictionary))


The below constraint, is the one that messes everything up. Once I remove [howManyIconView(iconViewWidth)] from that 1 line of code, everything is working fine.


howManyContentView.addConstraints(
    NSLayoutConstraint.constraintsWithVisualFormat(
        "H:[howManyIconView(iconViewWidth)][howManyInputField(inputFieldWidth)]-32-|", options: nil, metrics: metricsDictionary, views: viewsDictionary))


Any help would be greatly appreciated! (I don't have any errors)
 
I have triple checked that howManyContentView, howManyIconView, and howManyIcon are not mentioned in any code not shown in this post.
 
I attempted to make yet another view called "howManyInputView", where I put the iconView and the textfield in, then putting that as the only constraint. But with the same result of my screen getting squished to the left. (It must have something to do with my iconView?
 
I also attempted removing everything that has to do with the icon inside my the iconView. Still doesn't work.

After that I went ahead and printed out the constraints for my problem view, using the following


println("Horizontal: \(howManyIconView.constraintsAffectingLayoutForAxis(UILayoutConstraintAxis.Horizontal))")

println("Vertical: \(howManyIconView.constraintsAffectingLayoutForAxis(UILayoutConstraintAxis.Vertical))")

Here's the output:

(not sure why, but the code editor on this forum deletes my text, so it's in raw text. Sorry)

___________________________________________________________________________________________
Horizontal: [<NSLayoutConstraint:0x17409da10 H:[UIView:0x17419eed0(50)]>]

Vertical: [<NSLayoutConstraint:0x17409d8d0 V:|-(0)-[UIView:0x17419eed0]   (Names: '|':UIView:0x17419ed30 )>, <NSLayoutConstraint:0x17409d920 V:[UIView:0x17419eed0]-(0)-|   (Names: '|':UIView:0x17419ed30 )>]
___________________________________________________________________________________________

The constraint in question should have both a positional constraint and width constraint.

Yet it only shows the sizing constraint? I tried seperating the two, but it still has the same issue.

I then tried removing the positioning constraint. And it printed out exactly the same output. Which means, for some reason, despite positioning the view correctly to the left of the textField, it doesn't recognise that constraint?

So I believe the root of my headaches, are that it doesn't recognise my horizontal constraint. How I go about fixing that, I have no clue. That's why I'm hoping someone on here can help me. (I asked on apple developer forums, and stack overflow with no answers. I have been waiting / trying things here and there to fix it, without luck for 4 days, but instead of waiting my time I continued with my app) I ran into the exact same problem in another view of my app (also a scrollView), only diference is that it's a vertical constraint causing the problem.

Any help would be really greatly appreciated!