Using Xcode 4.2/iOS5.0
I have a viewController and an associated viewController.xib. The viewController is set up as the .xib’s file’s owner. It is invoked from a UINavigationController.
I am attempting some manual layout when the device is rotated portrait/landscape
As I understand it the UIView method to override is
- (void)layoutSubviews;
this is where you do the layout
But – as with drawRect – you do not call this directly, it is invoked when appropriate by iOS
Instead you call setNeedsLayout…
So I have this in the ViewController
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)
fromInterfaceOrientation {
[[self view] setNeedsLayout];
}
and in the same ViewController I have this
- (void)layoutSubviews {
//.manual override of automatic layout on rotation
NSLog(@"layoutSubviews");
}
However layoutSubviews does not get called and I don’t understand why.
Here is a clue…
If I call layoutSubviews directly as [self layoutSubviews]
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)
fromInterfaceOrientation {
[self layoutSubviews];
}
it does trigger
But if I call it as [[self view] layoutSubviews] it does not
So it appears that the view and it’s controller are not wired up correctly?
Your problem here is you’ve implemented
-layoutSubviewson your view controller, instead of on your view. It will never be called this way.-layoutSubviewsis a mechanism by which a custom view can lay out its own subviews (e.g. aUIButtonthat has aUIImageViewfor the image and aUILabelfor the label, and uses-layoutSubviewsto ensure the imageview and label are all positioned correctly). It’s not a mechanism by which a view controller can control the layout of its views.If you want to change layout on rotation, then you should just go ahead and set up the new layout inside of
-didRotateFromInterfaceOrientation:.