If I have a pointer to a UIViewController, can I be notified when it changes interfaceOrientation without modifying the code of the controller?
Is my best bet to detect changes in device orientation and then see if the UIViewController will/has rotate(d)?
You can use NSNotificationCenter :
[[NSNotificationCenter defaultCenter] addObserver:self // put here the view controller which has to be notified
selector:@selector(orientationChanged:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
- (void)orientationChanged:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//do stuff
NSLog(@"Orientation changed");
}
You can use the willAnimateRotationToInterfaceOrientation:duration:
method on your UIViewController and then reposition any UIViews (or any other code) for landscape or portrait. E.g.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
// change positions etc of any UIViews for Landscape
} else {
// change position etc for Portait
}
// forward the rotation to any child view controllers if required
[self.rootViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With