Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Introduction to Auto Layout in iOS Visual Format Language Blues and Purples Again!

Dmitri LaCour
PLUS
Dmitri LaCour
Courses Plus Student 10,545 Points

VFL throwing "unrecognized selector sent to instance" error in Xcode 9.2

I am working with Xcode 9.2 on the Visual Format Language portion of Introduction to Auto Layout in iOS course and am having issues setting up my purple and blue views. The code is below and the areas causing an issue are the second and third view.addConstraints. I've looked for typos and compared it against the code in the video but can't seem to fix it. What am I missing?

Also, Xcode forced me to add as AnyObject to my metrics dictionary. Can anyone explain why?

orangeView.backgroundColor = UIColor(displayP3Red: 255/255, green: 148/255, blue: 0, alpha: 1)
purpleView.backgroundColor = UIColor(displayP3Red: 187/255, green: 44/255, blue: 162/255, alpha: 1)
blueView.backgroundColor = UIColor(displayP3Red: 122/255, green: 206/255, blue: 255/255, alpha: 1)

orangeView.translatesAutoresizingMaskIntoConstraints = false
purpleView.translatesAutoresizingMaskIntoConstraints = false
blueView.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(orangeView)
view.addSubview(purpleView)
view.addSubview(blueView)

let views: [String: AnyObject] = [
            "orangeView": orangeView,
            "purpleView": purpleView,
            "blueView": blueView,
            "topMarginsGuide": view.layoutMarginsGuide.topAnchor
        ]

        let metrics: [String: AnyObject] = [
            "orangeViewWidth": 200 as AnyObject,
            "orangeViewHeight": 57 as AnyObject,
            "standardOffset": 8 as AnyObject,
            "bottomSpaceOffset": 50 as AnyObject
        ]

        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[orangeView(orangeViewWidth)]", options: [], metrics: metrics, views: views))
        orangeView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[topMarginsGuide]-standardOffset-[purpleView]-standardOffset-[orangeView(orangeViewHeight)]-bottomSpaceOffset-|", options: [], metrics: metrics, views: views))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[topMarginsGuide]-standardOffset-[blueView]-standardOffset-[orangeView]", options: [], metrics: metrics, views: views))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-standardOffset-[purpleView(==blueView)]-standardOffset-[blueView]-standardOffset-|", options: [], metrics: metrics, views: views))

1 Answer

You need to active this by adding .active = true at the end of the code

orangeView.centerXAnchor.constraint(equalTo: view.centerXAnchor).active = true