views:

367

answers:

1

I want my UIView subclass to behave so that when it is fitted in portrait orientation is has a certain size and another size when fitted in landscape mode.

Is it possible to have the view indicate to the view controller that's resizing it when the orientation changes that it has this "ideal size"?

To clarify, I'm confident that this is not something that can be done with the autoresizing mask. I also thought that sizeThatFits: would be what I need but I didn't see it get called when the orientation changed.

I'm also aware that I can get this done by overriding layoutSubviews of the superview (or maybe some other method of the view controller but I would like to have this behavior embedded in the view to facilitate reuse.

+2  A: 

You get several messages when the device rotates:

- shouldAutorotateToInterfaceOrientation:;
- willRotateToInterfaceOrientation:duration:;
- didRotateFromInterfaceOrientation:

You can use these to re-layout your view when the user rotates.

If you don't want to use a view controller, you can register for the UIDeviceOrientationDidChangeNotification notification.

Ben Gottlieb
Thanks but AFAIK these are methods of the view controller and not the view which means that the view doesn't stand alone which is what I'm looking for.
Eyal Redler
Correct. I've amended my answer to discuss the UIDeviceOrientationDidChangeNotification.
Ben Gottlieb