8

My aim is to have 3 images shrink, grow, and move along a horizontal axis depending on selection. Using Auto Layout seems to make the images jump about as they try to fulfil the Top space to superview / Bottom space to superview constraints.

So to combat this I have put all the images inside their own UIView. The UIView is set to the maximum size the images can grow to, it is centred on the horizontal axis. So now all the images must do is stay centred inside their corresponding UIView. This has fixed my problem as the UIViews perform the horizontal translation, while the images shrink/grow inside while remaining centred. My question is - is this the correct way to do this? It seems very long and like I am perhaps misusing the ability of Auto Layout. I have to perform similar tasks with more images and so any advice is welcome! Thanks.

bean
  • 1,091
  • 1
  • 12
  • 23

1 Answers1

9

I've just written a little essay on this topic here:

How do I adjust the anchor point of a CALayer, when Auto Layout is being used?

Basically autolayout does not play at all well with any kind of view transform. The easiest solution is to take your view out of autolayout's control altogether, but alternatively you can give it only constraints that won't fight back against the particular kind of transform you intend to apply. That second solution sounds like just the sort of thing you're doing.

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for your help. Yes. The 2nd solution is what appeals more to me! – bean Jan 02 '13 at 13:41
  • I use CATransform3D translations on views that are part of autolayout without problems. – bandejapaisa Sep 04 '13 at 15:22
  • @bandejapaisa No, you do not. CATransform3D is applied to layers, not views. See solution 4 of my essay referred to above (http://stackoverflow.com/questions/12943107/how-do-i-adjust-the-anchor-point-of-a-calayer-when-auto-layout-is-being-used/14105757#14105757) - that solution is exactly to use layer transforms instead of view transforms. – matt Sep 04 '13 at 18:51
  • @matt OK you're pulling me up on a slight typo.... Obviously you know every view is backed by a layer, and I meant to say I use the CATransform3D on the views layer. Beside the point, my views are using auto layout and I transform their layers which accomplishes the same end result - with no problems. I do this and it works. – bandejapaisa Sep 04 '13 at 19:42
  • 2
    @bandejapaisa You're not listening. I said (in my original answer) that autolayout does not play well with a view transform. And I'm right; it doesn't. You're not doing a view transform; you're doing a layer transform. That's fine, but it's irrelevant to what I said. Layer transforms work fine with autolayout, because autolayout acts upon views, not layers. – matt Sep 05 '13 at 23:24
  • @matt Don't get your knickers in a twist. I do completely understand you and I am listening. I agreed with you and admitted I made a typo referring to view transforms when I meant layer transforms. I'm not saying your answer is incorrect. I would improve your correct answer with a mention of layer transforms for those unaware of the difference - as you did in the other post you mentioned. – bandejapaisa Sep 06 '13 at 10:51
  • 1
    @bandejapaisa Sorry for the delayed response but I was busy untwisting my knickers! - Okay, there's two issues here. (1) Translation transforms are not such a problem in any case. Scale transforms, however, will show that we are not scaling from the center like we're supposed to, because autolayout moves the view incorrectly. (2) Applying a scale transform to a view's layer won't save you, except temporarily. It doesn't trigger immediately layout, so it works correctly, but when layout does come along, it will move the view incorrectly. – matt Sep 06 '13 at 19:47