Reputation: 38150
every UIViewController has a method called willRotateToInterface.
Is it possible to do this within a UIView too?
Does this match the idea of model view controller ?
The only way I can think of is to send the event from the UIViewController to the UIView.
Is there a global variable for the current orientation?
Upvotes: 6
Views: 6002
Reputation: 100120
If you just want to adjust view to new size/layout when orientation changes, you should just override its layoutSubviews
method.
It will be called whenever size of the view changes, which usually happens when view is rotated.
Upvotes: 3
Reputation: 22395
You can subscribe to a global notification and get a call when the device is rotated, it wont do anything for you though..
[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
Upvotes: 2
Reputation: 299345
Observe UIDeviceOrientationDidChangeNotification
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
...
- (void)orientationDidChange:(NSNotification *)note
{
NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]);
}
I should note that yo uneed to add -beginGeneratingDeviceOrientationNotifications
when you want these notifications to be sent, and call -endGeneratingDeviceOrientationNotifications
when you want them to stop. There is a battery impact of generating these, so you should only do so when your view is on screen. UIViewController
does all this for you, so if you have a view controller, it is worth letting it do the work.
Upvotes: 17