如何以横向模式正确打开我的应用程序

Posted

技术标签:

【中文标题】如何以横向模式正确打开我的应用程序【英文标题】:How to correctly open my application in landscape mode 【发布时间】:2016-04-11 08:13:39 【问题描述】:

我有一个视图控制器,我在其中添加了一个 UIView 全屏大小,在那个 UIView 中我有 AVCapturesession 可以帮助我捕捉照片, 我的视图控制器在纵向模式下打开良好,但在横向模式下突然打开。

代码如下,

- (void)viewDidLoad 
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor whiteColor];
  [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
  self.camera.userInteractionEnabled = YES;
  UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
  pinchGesture.delegate=self;
  [self.camera addGestureRecognizer:pinchGesture];
  [self.navigationController setNavigationBarHidden:YES animated:YES];

相机是 UIView,它是我的 UIViewController 的属性,

再次,

-(void) viewDidAppear:(BOOL)animated

  AVCaptureSession *session = [[AVCaptureSession alloc] init];
  session.sessionPreset = AVCaptureSessionPresetMedium;

  CALayer *viewLayer = self.camera.layer;
  NSLog(@"viewLayer = %@", viewLayer);
  captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

  captureVideoPreviewLayer.frame = self.camera.layer.bounds;
  [self.camera.layer addSublayer: captureVideoPreviewLayer];
  device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  NSError *error = nil;
  AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
  if (!input) 
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
  
  [session addInput:input];

  [session startRunning];
  stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
  NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
  [stillImageOutput setOutputSettings:outputSettings];

  [session addOutput:stillImageOutput];

  isUsingFlash = NO;
  isUsingFrontFacingCamera = NO;
  effectiveScale = 1.0;

我的视图在横向模式下打开错误,一旦我旋转到纵向它会很好,再次旋转到横向它工作良好,只是它在横向模式下无法正常启动,为什么?

这里我设置的是根视图控制器,

 sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
 cameraFirstController = [sb instantiateViewControllerWithIdentifier:@"FirstViewController"];
 cameraFirstController.delegate = self;
 nav = [[CustomNavigationController alloc]initWithRootViewController:cameraFirstController];
 [self.viewController presentViewController:nav animated:YES completion:nil];

【问题讨论】:

你能在设置 rootViewController 的地方添加代码吗? 我已在您编辑的答案中添加了代码。请检查 【参考方案1】:

似乎问题是设置窗口时的调用顺序。在分配rootViewController 之前,您需要调用makeKeyAndVisible

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
self.window.rootViewController = self.YourViewController;

【讨论】:

以上是关于如何以横向模式正确打开我的应用程序的主要内容,如果未能解决你的问题,请参考以下文章

如何将视图从纵向模式旋转到横向模式

如何使用 javascript/Extjs 进行默认横向打印?

如何为android中的xml文件调整纵向和横向模式的正确界面

如何以编程方式将我的 android 应用程序锁定为纵向/横向模式?

UIActionSheet 在横向模式下无法正确显示

如何以横向自动打开 SWRevealViewController 侧面板