Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Interface Rotation on UIViewController even if not defined in - (NSUInteger)supportedInterfaceOrientations

Tags:

ios6

I have a UIViewController handling several UIImageViews on the main view. On the bottom is a UIToolbar with a few items to interact with.

Now, when I rotate the device, I don't want the viewController to rotate, but just the UIImageViews. In other words, the toolbar at the bottom would be on the left (or the right), but the imageViews would be rotated correctly.

So, by using the methods

- (BOOL)shouldAutoRotate {
   return YES;
}

combined with

- (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskPortrait;
}

and

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// rotate the image views here
}

any rotation on the device will not be executed, because only one interface orientation is supported (UIInterfaceOrientationMaskPortrait). But when I add another interface orientation to be supported in the supportedInterfaceOrientations-method, the view controller will rotate too.

How can I detect a rotation for a view controller, even if only one orientation is supported? Or is there another possibility to rotate UIViews based on the changing device orientation?

Thanks for any help!

like image 783
uruk Avatar asked Jan 17 '13 20:01

uruk


1 Answers

Try to use the UIDevice instance to detect changes in the device’s physical orientation. To begin receiving notification you can use something like this (in viewWillAppear: method for example):

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    //No reason to ask NSNotification because it many cases `userInfo` equals to
    //@{UIDeviceOrientationRotateAnimatedUserInfoKey = 1;}
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceDidRotate) name:@UIDeviceOrientationDidChangeNotification object:nil];
}

For unregistering receiving device rotation events, use this (in viewWillDisappear: for example):

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

And this is an example of deviceDidRotate function:

- (void)deviceDidRotate {
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    switch (orientation) {
        case UIDeviceOrientationPortrait:
        case UIDeviceOrientationPortraitUpsideDown:
            // do something for portrait orientation
            break;
        case UIDeviceOrientationLandscapeLeft:
        case UIDeviceOrientationLandscapeRight:
            // do something for landscape orientation
            break;

        default:
            break;
    }
}
like image 180
slavik Avatar answered Oct 03 '22 08:10

slavik