Alex Cio
Alex Cio

Reputation: 6052

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....

Upvotes: 2

Views: 6826

Answers (4)

Vyachaslav Gerchicov
Vyachaslav Gerchicov

Reputation: 2457

From previous answer:

[[UIDevice currentDevice] setValue:
           [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];

WARNING

I haven't a possibility to comment this answer so I'll edit it.

The author of this answer tries to set a value of UIInterfaceOrientation type to a variable of UIDeviceOrientation. They are both NSInteger enums, so compiler will not show a warning/error. But they have different meanings and "device" additionally has "face up" and "face down" orientations.

Upvotes: 0

iOSDev
iOSDev

Reputation: 995

Use this works perfectly on iOS7 and earlier.

[[UIDevice currentDevice] setValue:
           [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];

Upvotes: 2

Crake
Crake

Reputation: 1679

[[UIApplication sharedApplication] setStatusBarOrientation:yourDesiredOrientation];

You will then need to edit the shouldAutoRotate method (following example is for portrait:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

Upvotes: 0

Eduardo Russo
Eduardo Russo

Reputation: 4219

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

Upvotes: 6

Related Questions