【问题标题】:Update interface orientation manually in iOS在 iOS 中手动更新界面方向
【发布时间】:2012-06-13 11:25:10
【问题描述】:

我的 iOS 应用程序支持除 PortraitUpsideDown 之外的所有方向。 但是在应用程序中,我有一个带有偏好的视图,我希望它只以纵向显示。因此,无论何时显示此视图,都会根据需要对其进行旋转,使其处于纵向模式。这意味着用户也将在纵向模式下旋转设备以设置首选项,然后在关闭此视图界面后现在应该具有纵向。 问题是,隐藏首选项视图后,界面保持横向,因为在显示此视图后我阻止了自动旋转。 因此,隐藏视图后,我想手动将界面更新为当前设备方向。我该怎么做?


self.view.hidden=NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.view.alpha=1.0;
[UIView commitAnimations];

此代码在其父视图上的 LongPressGesture 之后从 OptionsViewController 调用。

【问题讨论】:

    标签: objective-c ios cocoa-touch orientation


    【解决方案1】:

    我创建了一个 UIViewController 扩展来强制更新控制器的方向,基于 Marek R 提出的解决方案。由于 iOS 的新版本,他的解决方案不再起作用。我在这里发布我的解决方案来强制更新方向(以考虑到控制器支持的方向方法),而不会产生副作用。希望对您有所帮助。

    UIViewController *vc = [[UIViewController alloc]init];
    [[vc view] setBackgroundColor:[UIColor clearColor]];
    [vc setModalPresentationStyle:(UIModalPresentationOverFullScreen)];
    [self presentViewController:vc animated:NO completion:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [vc dismissViewControllerAnimated:YES completion:completion];
        });
    }];
    

    【讨论】:

      【解决方案2】:

      您所要做的就是将以下内容添加到您正在使用的视图控制器中以满足您的偏好。

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
          return (interfaceOrientation == UIInterfaceOrientationPortrait);
      }
      

      【讨论】:

      • 我已经尝试过了。但它似乎不起作用,因为这个视图是主视图的子视图,它支持所有方向。所以如果我的主视图会自动旋转,偏好视图也会旋转。还是我误解了什么?
      • 所以这个视图是你视图控制器中self.view中的一个子视图?您只希望这个特定的子视图旋转而其他视图保持静止?这就是你想要的吗?
      • 不,我有一个由 MainViewController 控制的 MainView 和一个作为 MainView 的子视图并由 OptionsViewController 控制的 OptionsView。我希望 MainView 自动旋转,但 OptionsView 在纵向模式下保持静态。
      • 您能否发布代码以显示您如何调用选项视图?
      • 我不太明白您所说的“调用选项视图”是什么意思。你的意思是我怎么让它出现?我不明白您对哪个代码感兴趣。我在 IB 中设置了视图。它是对应的 OptionsViewController 的 view 属性,我从中访问视图。
      【解决方案3】:

      调用此解决方法对我有用:

      -(void)forceOrientationUpdate {
          UIViewController *c = [[UIViewController alloc]init];
          [self presentModalViewController:c animated:NO];
          [self dismissModalViewControllerAnimated:NO];
          [c release];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多