如何在按钮点击时更改屏幕的方向?

时间:2017-05-09 06:39:08

标签: ios objective-c iphone

我从这个网站和其他人那里尝试过很多代码....但似乎没有什么对我有用。这是我的代码......

- (IBAction)orientation:(id)sender {
    self.currentDeviceOrientation = [[UIDevice currentDevice] orientation];
    int cur = self.currentDeviceOrientation;
    int port = UIInterfaceOrientationPortrait;
    int landl = UIInterfaceOrientationLandscapeLeft;
    int landr = UIInterfaceOrientationLandscapeRight;

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        NSNumber *value= [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"orientation"];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft  animated:YES];
    }
}

点击一个按钮,我希望屏幕从当前方向改变,即,如果屏幕处于纵向模式,它应该改为横向和反面。

2 个答案:

答案 0 :(得分:0)

您需要根据需要设置方向。

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

答案 1 :(得分:0)

尝试此操作并根据需要进行更改。

- (IBAction)orientation:(id)sender {

        if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){

        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        NSLog(@"landscape left");

        }

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){

        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        NSLog(@"landscape right");

        }

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){

        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        NSLog(@"portrait");

        }


}