7

I inherited an overly complicated project (so I don't know all of the inner workings), and I'm running into a bug. Certain parts of my app have some long animations done with CATransaction, and it seems to be causing layoutSubviews to be called repeatedly while the animations are active. This doesn't happen on ios5 and everything looks correct, but on ios6 it gets called nonstop and interferes with a lot of the layout of the view. The stack trace is all hidden/grayed out, but it does seem to begin with CA::Transaction::commit()

Did anything with CATransaction change between ios versions to cause something like this?

Chris C
  • 3,221
  • 1
  • 27
  • 31
  • 1
    are you animating an uiview with autoresizesSubviews set to true? – mxb Jul 07 '14 at 08:33
  • Is it using autolayout? That was introduced in ios6. If you do certain things that call layout, or do things in the wrong place you will get a loop or get essentially relayout way more often than needed. You first want to look for spurious setNeedsLayout or layout calls. – uchuugaka Jul 07 '14 at 09:44
  • Have you checked this question? http://stackoverflow.com/questions/728372/when-is-layoutsubviews-called – arturdev Jul 13 '14 at 15:17

2 Answers2

2

See this post: UIView/CALayer: Transform triggers layoutSubviews in superview

Apple answered me via TSI:

why am I seeing this behavior? is this inconsistency or am I misunderstanding some core concepts?

A view will be flagged for layout whenever the system feels something has changed that requires the view to re-calculate the frames of its subviews. This may occur more often than you'd expect and exactly when the system chooses to flag a view as requiring layout is an implementation detail.

why does it cascade upwards the view hierarchy?

Generally, changing a geometric property of a view (or layer) will trigger a cascade of layout invalidations up the view hierarchy because parent views may have Auto Layout constraints involving the modified child. Note that Auto Layout is active in some form regardless of whether you have explicitly enabled it.

how can I avoid superview to layoutSubviews every time I'm changing transform?

There is no way to bypass this behavior. It's part of UIKit's internal bookkeeping that is required to keep the view hierarchy consistent.

Community
  • 1
  • 1
hfossli
  • 22,616
  • 10
  • 116
  • 130
  • I saw this post already, but i don't understand how to work around this. In my case i have a custom control where the view tree is A->B I'm animating the B.layer and i need to setup the frame of the B in the layoutSubviews of A. But layoutSubviews of A is called when i animate B.layer – Luca Bartoletti Aug 05 '14 at 15:48
0

Sounds like an Autolayout issue. Does the view or any of its subviews use Autolayout? Autolayout is nice but doesn't seem very fast and efficient so may cause issues when animating.

Of course it may be necessary for the subviews to be layed out each step in the animation if the size one shape of the view is changing in a way that affects subview placement or size. Consider the animation and what effects it has.

Joseph Lord
  • 6,446
  • 1
  • 28
  • 32