When rotating a scroll view inside a UIView, the scroll view doesnt position correctly using its default autoresizing behaviour.
Thus, when rotation occurs, in willRotateToInterfaceOrientation
i call [self.view setNeedsLayout];
and my layoutSubviews
method is as follows:
- (void)layoutSubviews {
NSLog(@"here in layoutSubviews");
}
But putting a breakpoint on the method, and it never seems to enter the method.
Do i need to do something else to get it to work?
Thanks.
You should override willAnimate... and set the new frame of your view. Layoutsubviews should be called automatically during rotation.
Calling
[someScrollView setNeedsLayout]
may not actually do anything if the view hasn't been resized or moved. As you say, that method is never called using the default autoresizing behavior, because the default behavior is to not resize at all. You most likely need to setsomeScrollView.autoresizingMask
. When the interface rotates, the view will resize itself, andlayoutSubviews
will get called.willRotateToInterfaceOrientation is being called before the orientation is changed, hence your UIView will still have the old size. Try using didRotateFromInterfaceOrientation instead.
Also, for added effect, I would hide the scrollView ( maybe with inside an UIAnimation block) in willRotateToInterfaceOrientation, resize it and then show it in didRotateFromInterfaceOrientation (again, maybe inside an animation block).
Here is a snippet from one of my apps:
You can even do fancy stuff by checking the new orientation using UIInterfaceOrientationIsPortrait(orientation) or UIInterfaceOrientationIsLandscape(orientation).
The method doesn't get called because a ViewController doesn't have a layoutSubviews method.
When you call
[self.view setNeedsLayout];
it will just call thelayoutSubviews
method of the view of the view controller:[self.view layoutSubviews]
.You will need to subclass the UIScrollview to make this work.
Actually, you probably want to override the view controller willAnimateRotationToInterfaceOrientation:duration: method (cut from an example of mine):
}