iPhone set interfaceorientation programmatically

心已入冬 提交于 2019-12-24 02:58:09

问题


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


回答1:


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




回答2:


Use this works perfectly on iOS7 and earlier.

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



回答3:


[[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);
}



回答4:


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.



来源:https://stackoverflow.com/questions/11109662/iphone-set-interfaceorientation-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!