BytesGuy
BytesGuy

Reputation: 4127

Using AutoLayout to just rotate a View

With AutoLayout, is it possible to keep a View (button, image, etc) in the same location but just rotate it 90 degrees when the device is rotated?

For example, in the following images the Views stay exactly as they were placed in the portrait orientation (same distances from the edges), but are rotated in landscape.

Portrait Landscape

Upvotes: 1

Views: 947

Answers (2)

cmyr
cmyr

Reputation: 2275

You describe the views in the images you've provided as "staying in exactly the same place", but I think this is to misunderstand autolayout and the rotation behaviour. Your layout has changed significantly between the two examples. Where both views were previously aligned along their left edges, now they're aligned along their bottom edges.

Basically: when you rotate the device (lets say from standard portrait to home-left-landscape) you're changing which view is the top, not the direction the top view is pointing.

If you want to recreate the look of the rotated view you provided, you have a few options. I'd suggest looking at the visual formatting language, which is a good way of adding constraints programatically... it's easier then it seems. Take a look at the iOS auto-layout talks from WWDC 2012 if you want a good introduction. You could then add and remove the appropriate constraints when the device rotates. (it might take a bit of playing around). There's also a section in the View Controller Programming Guide on 'creating a custom landscape orientation' that might be helpful.

Upvotes: 2

rob mayoff
rob mayoff

Reputation: 385600

If you're allowing the interface orientation to rotate, then you'll have to change your constraints on rotation to put the views where you want them.

If you're not allowing the interface orientation to rotate, then you'll have to subscribe to device orientation change notifications. When the orientation changes, you can update the transforms of the views to rotate them. If the views are square, that should suffice. If the views are not square, you also need to modify your constraints based on the rotated frames.

Upvotes: 2

Related Questions