tags:

views:

4507

answers:

6

I have a custom view that's not getting layoutSubview messages during animation.

I have a view that fills the screen. It has a custom subview at the bottom of the screen that correctly resizes in IB if I change the height of the nav bar. layoutSubviews is called when the view is created, but never again. My subviews are correctly laid out. If I toggle the in-call status bar off, the subview's layoutSubviews is not called at all, even though the main view does animate its resize.

Under what circumstances is layoutSubviews actually called?

I have autoresizesSubviews set to NO for my custom view. And in IB I have the top and bottom struts and the vertical arrow set.

+1  A: 

have you looked at layoutIfNeeded?

The documentation snippet is below. Does the animation work if you call this method explicitly during the animation?

layoutIfNeeded Lays out the subviews if needed.

- (void)layoutIfNeeded

Discussion Use this method to force the layout of subviews before drawing.

Availability Available in iPhone OS 2.0 and later.

Willi Ballenthin
+1  A: 

I tracked the solution down to Interface Builder's insistence that springs cannot be changed on a view that has the simulated screen elements turned on (status bar, etc.). Since the springs were off for the main view, that view could not change size and hence was scrolled down in its entirety when the in-call bar appeared.

Turning the simulated features off, then resizing the view and setting the springs correctly caused the animation to occur and my method to be called.

An extra problem in debugging this is that the simulator quits the app when the in-call status is toggled via the menu. Quit app = no debugger.

Steve Weller
Are you saying that layoutSubviews is called when the view is resized? I always assumed it is not...
Andrey Tarantsov
It is. When a view is resized it needs to do something with its subviews. If you don't provide it then it moves them automatically using springs, struts etc.
Steve Weller
A: 

Another part of the puzzle is that the window must be made key:

[window makeKeyAndVisible];

of else the subviews are not automatically resized.

Steve Weller
+1  A: 

When migrating an OpenGL app from SDK 3 to 4, layoutSubviews was not called anymore. After a lot of trial and error I finally opened MainWindow.xib, selected the Window object, in the inspector chose Window Attributes tab (leftmost) and checked "Visible at launch". It seems that in SDK 3 it still used to cause a layoutSubViews call, but not in 4.

6 hours of frustration put to an end.

Tal Yaniv
Did you make the window key? If not, this can cause all sort of interesting things to not happen.
Steve Weller
A: 

You are correct.. I had to select the Title bar of the window to get at the settings you talked about.

Symptoms = black render buffer screen. I fiddled with all the render init and resizeLayer settings.. Upon inspecting my Console, I found log entries for resizeLayer missing. I also changed the clear screen color to blue, but the screen remained black.

William Thompson
A: 

Thanks Steve Weller,

[window makeKeyAndVisible];

It is really works fine.

Sergey