【问题标题】:Pragrammatically set device orientation for particular viewcontroller以实用的方式为特定视图控制器设置设备方向
【发布时间】:2018-02-07 05:26:25
【问题描述】:

我想以编程方式将设备方向设置为横向模式到特定的视图控制器,如果我与 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];

上面的代码运行良好,但我想强制下一个控制器处于横向模式而不询问警报,所以直接在按钮单击时编写代码,如下所示,

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

【问题讨论】:

    标签: ios objective-c ios10


    【解决方案1】:

    试试这个强制视图在横向模式下快速启动: 在 viewWillAppear 中使用此代码

    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(Int(value), forKey: "orientation")
    

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • [UIViewController attemptRotationToDeviceOrientation]; 应该在上述行之后调用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多