Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView layoutSubviews not being called

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?

like image 462
user1054941 Avatar asked Oct 14 '25 03:10

user1054941


1 Answers

Your problem here is you've implemented -layoutSubviews on your view controller, instead of on your view. It will never be called this way. -layoutSubviews is a mechanism by which a custom view can lay out its own subviews (e.g. a UIButton that has a UIImageView for the image and a UILabel for the label, and uses -layoutSubviews to 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:.

like image 132
Lily Ballard Avatar answered Oct 19 '25 13:10

Lily Ballard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!