Gold challenge with AutoLayout


#1

As I feel that Autosizing becomes obsolete with the introduction of AutoLayout, I have configured the user interface with AutoLayout. The automatic constraints settings are almost perfect, I had to add a constraint to get the autosizing of the image correct.

But on topic: the gold challenge seems quite easy to solve with AutoLayout. Easier than expected! In willAnimateRotationToInterfaceOrientation:duration: the only thing I entered was checking if the orientation is set to landscape and change the center of the ‘gold’ button to centered at the right edge of the screen.

When I tested the result this works perfectly. But to my surprise when turning the orientation back to portrait the ‘gold’ button’s original position is magically restored, I didn’t add any code for this.

Can anybody explain me the magic behind this to me?

Thanks, Frank


#2

Apple has changed the complete autorotation behavior. A lot more information to this topic is found there:
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html

Additionally, AutoLayout is designed to be much more powerful than autosizing, to avoid having to write the same boring things in code over and over again. AutoLayout is able to deal with different content sizes (imagine translation to german :smiley: They always say it is terribly long… Thus, as a German, I can only confirm this partially). AutoLayout can automatically solve conflicts, if it cannot satisfy all constraints. AutoLayout is able to handle rotation by just either resizing the content or breaking lower-priority constraints. AutoLayout is great.

You will find more practical information than in the documentation on the „magic” that AutoLayout does in this tutorial:
http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2


#3

Another nice fact: Since xCode 5, it is possible to have a preview of your view in Landscape, or iOS6 or whatever you want. In the very left upper corner of the editor area, there is a small rectangular button, in which you see options for related editors. For example, if you have a „.h” file open, you could klick there and find the „.m” file, or the classes which call your classes methods. Extremely useful.

In case you have opened a Storyboard or nib file, you are able to open a preview through this menu (with click in the main editor, alt-click in the assistant editor). At the bottom of this preview there are several options like orientation and iOS version.
It´s a lot more fun to debug view layouts with that!


#4

[quote=“TAKeanice”]Another nice fact: Since xCode 5, it is possible to have a preview of your view in Landscape, or iOS6 or whatever you want. In the very left upper corner of the editor area, there is a small rectangular button, in which you see options for related editors. For example, if you have a „.h” file open, you could klick there and find the „.m” file, or the classes which call your classes methods. Extremely useful.

In case you have opened a Storyboard or nib file, you are able to open a preview through this menu (with click in the main editor, alt-click in the assistant editor). At the bottom of this preview there are several options like orientation and iOS version.
It´s a lot more fun to debug view layouts with that![/quote]

Great info, thanks!