Alex Cio Alex Cio - 2 months ago 19
iOS Question

iPhone set interfaceorientation programmatically

I am trying to change the orientation of a single

UIViewController
programmatically. The user should choose in the settings if the view will be portrait or not. The value will be saved in
NSUserDefaults
and in the specific
ViewController
the orientation should be changed. So if Portrait is on the
interfaceOrientation
should be on
UIInterfaceorientationUpsideDown
......

I used following method but it will only change the orientation when the user will turn the device to landscape/portrait... otherwise nothing will be triggered.

- (BOOL)shouldAutoRotateToInterfactOrientation:(UIInterfaceOrientation)interfaceOrientation


Furthermore i tryied the next one (also with method name "
setOrientation
"):

[[UIDevice currentDevice] orientation: UIDeviceOrientationPortraitUpsideDown];


But again, nothing happens, there just appear errors because xcode sayes there are no methods with this name.

But ther should be another way to change it programatically...
the first method is also just a method that handles the actionListener after device position but there should be a way to call this
actionListener
in a direct way....
I just looked inside the
UIApplication
File but there is no usefull method....

Answer Source

You should use something like this code:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(rotationChanged:)
                                             name:@"UIDeviceOrientationDidChangeNotification"
                                           object:nil];

And then, change your view according to the orientation:

-(void)rotationChanged:(NSNotification *)notification{
    NSInteger orientation = [[UIDevice currentDevice] orientation];
    UIWindow *_window = [[[UIApplication sharedApplication] delegate] window];
    switch (orientation) {
        case 1:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];

            [_window setTransform:CGAffineTransformMakeRotation (0)];
            [_window setFrame:CGRectMake(0, 0, 320, 480)];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

            [UIView commitAnimations];
            break;
        case 2:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];

            [_window setTransform:CGAffineTransformMakeRotation (M_PI)];
            [_window setFrame:CGRectMake(0, 0, 320, 480)];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

            [UIView commitAnimations];
            break;
        case 3:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];

            [_window setTransform:CGAffineTransformMakeRotation (M_PI / 2)];
            [_window setFrame:CGRectMake(0, 0, 320, 480)];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];

            [UIView commitAnimations];
            break;
        case 4:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];

            [_window setTransform:CGAffineTransformMakeRotation (- M_PI / 2)];
            [_window setFrame:CGRectMake(0, 0, 320, 480)];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

            [UIView commitAnimations];
            break;
        default:
            break;
    }
}

Hope it helps :P