Is there a way to catch the WillRotateToInterfaceOrientation event from a UIView?

each UIViewController has a method called willRotateToInterface.

Is it possible to do this in UIView?

Is this consistent with the idea of ​​the model view manager?

The only way I can think of is to send an event from a UIViewController to a UIView.

Is there a global variable for the current orientation?

+6
objective-c iphone uiviewcontroller uiview
source share
3 answers

Watch UIDeviceOrientationDidChangeNotification :

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; ... - (void)orientationDidChange:(NSNotification *)note { NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]); } 

UIDevice class reference

It should be noted that you need to add -beginGeneratingDeviceOrientationNotifications when you want these notifications to be sent, and call -endGeneratingDeviceOrientationNotifications when you want them to stop. This leads to a battery effect, so you should only do this when your screen is displayed on the screen. UIViewController does all this for you, so if you have a view controller, you should let it do the job.

+17
source share

If you just want to adjust the view to a new size / layout when changing orientation, you just have to override its layoutSubviews method.

It will be called whenever the view is resized, which usually happens when the view is rotated.

+3
source share

You can subscribe to the global notification and receive a call when the device is turned, it will not do anything for you, though ..

 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 
+2
source share

All Articles