Mastering Auto Layout - Part 3: Constraint Priorities | Ray Wenderlich

Learn how to modify the priorities of constraints to create a powerful hierarchy that can handle multiple different situations.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3933-mastering-auto-layout/lessons/3

Hi Jerry,

Thanks for your great series on Auto-layout. However I cannot download your course materials for the Mastering episodes. I get an error stating: The request could not be satisfied. CloudFront is currently experiencing problems with requesting objects from Amazon S3.
Generated by cloudfront (CloudFront)
Request ID: 044zcAkecsQ4YHCagjUPuYlGt5-PGAHRUpnESJCBCw-r5ONzFVob3w==

Can you find out what’s going on?

Never mind, it was the Amazon outage… it wrecked havoc may sites!

Hi Jerry,

in the challenge, the red arrow is flowing nicely on the screen.
We have set up constraints “Higher than or equal to zero”, so that the arrow always stay on the screen. The constraint is between the arrow and the top view, but not the top view MARGIN.

When i change the constraint from Top view to Top view margin, just to try it out, not only the arrow does not stay on screen anymore, but the coordinates become messed up and the arrow will misbelieve it is at the correct place when it is not (ie. it will get in the middle on the screen at a place that is not the one where it is supposed to)

Why the fact that we change to View Margin instead of simply View will make such a mess ?

Thanks a lot if you can answer

Hm, I can’t think of why changing to the margin would do this. If you want to upload the project somewhere, I’ll take a look when I can.

Hi, just open the “finished challenge” project,
select the constraint between the arrow and the top view, change from top view to top view margin (for both the leading and trailing)

Hi Jerry, i made a 1 minute video showing it

Whew! Only took me 8 tries to get this to work. I never could get the arrow to move at the end of page 8 after trying probably 6 times so I gave up and decided to see if adding the code would work and that finally did the trick.

Also, there is a possible typo on page 2:

“Top, left, bottom, and right constraints of 0 to the view. Deselect the Constraint to Margins checkbox and make sure the bottom constraint is to the view, not the bottom layout guide.”

The challenge finished project is using “bottom layout guide” and not “view” I used bottom layout guide as well and mine works fine.

I had a slight problem with terminology of the hierarchy, i.e. top view vs super view etc etc. but it finally came together.

Good stuff!

Hi Jerry,

I select the Marker view and delete the “Trailing Space to: View >= 0” constraint, then I tried to add that constraint back using “Add New Constraint” and click the trailing space down arrow, it only shows “Scroll View” that I can add constraint to. How can I add “Trailing Space” constraint to “View”?

@jerbeers Do you have any feedback regarding all of this? Thank you - much appreciated! :]

There are several different ways to create constraints between two views, the “Add New Constraints” button is just one. In this case, in the document outline, I’d control-drag from the marker view to the top-level view and create the constraint that way.

I tried several variations for printing out the constraints on a button including:

po [0x7fb67fd07f20 constraints]
po 0x7fb67fd07f20.constraints
po constraints(0x7fb67fd07f20)

In all cases I get "is not a valid command. error: Unrecognized command " What it the proper command for XCode 10 with Swift 4?

This is a bit complicated from Swift. Since it is a type-safe language, you can’t just pass a message to a memory location, like you can with Objective-C. There are two options to solve the problem:

  1. Tell LLDB (the debugger) to use Objective-C:
    expr -l objc++ -O -- [0x7fb67fd07f20 constraints]
    This is just like po, but with a “language” parameter, telling it to use objc++ for the language

  2. Tell Swift the kind of object you have:
    po unsafeBitCast(0x7fb67fd07f20, to: UIButton.self).constraints
    This tells LLDB what type of object you have at that memory address, so it can get the property from it

1 Like

Wow! That’s a bit of wizardry. Thanks!

Hey, cool challenge, i am not able to figure out why we used the value 1610 while constraining marker to superview.

Nevermind, figured it out myself.