语法设置特定viewcontroller的设备方向

时间:2018-02-07 05:26:26

标签: ios objective-c ios10

我想以编程方式将设备方向设置为横向模式到特定的viewcontroller,如果我使用UIAlertController,它的工作情况很好,有YES / NO选项,但如果我在按钮点击时直接写相同的代码。它不起作用。

使用UIAlertController工作代码,

 UIAlertController * alert=[UIAlertController
alertControllerWithTitle:APP_NAME message:@"Please Select Video Orientation."preferredStyle:UIAlertControllerStyleAlert];

 UIAlertAction* yesButton = [UIAlertAction
                                    actionWithTitle:@"Landscape Mode"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {
                                        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
                                        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
                                        [self.navigationController pushViewController:mySecondViewController animated:NO];
                                    }];
 UIAlertAction* noButton = [UIAlertAction
                                   actionWithTitle:@"Portrait Mode"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action)
                                   {
                                       NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
                                       [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
                                       [self.navigationController pushViewController:mySecondViewController animated:NO];
                                   }];

 [alert addAction:yesButton];
 [alert addAction:noButton];

 [self presentViewController:alert animated:YES completion:nil];

上面的代码运行良好,但是我想在横向模式下强制使用下一个控制器,而不需要使用Alert,所以直接在按钮点击上编写代码就像这样,

  NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
  [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
  [self.navigationController pushViewController:mySecondViewController animated:NO];

但是同样的代码效果不好。它将首先以纵向打开mySecondViewController并手动我们必须倾斜手机一次然后它将移动到横向。

我尝试了很多选择,但没有运气。使用iOS 10.1在iPhone5和iPhone6上进行测试

更新:添加mySecondViewController代码 这是第二个视图控制器代码,

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}

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

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

-(BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

2 个答案:

答案 0 :(得分:1)

尝试使用此选项强制视图在swift中以横向模式启动: 在viewWillAppear

中使用此代码
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(Int(value), forKey: "orientation")

override func shouldAutorotate() -> Bool {
    return true
}

答案 1 :(得分:0)

这是设置方向的代码,但如果您想要特定的VC,请在该VC类中尝试此代码

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