【问题标题】:shouldAutorotateToInterfaceOrientation in subview子视图中的 shouldAutorotateToInterfaceOrientation
【发布时间】:2012-07-30 03:26:05
【问题描述】:

我在旋转设备时尝试更改视图布局时遇到了一些麻烦,我有一个名为 OverviewViewController 的主视图,它包含 2 个视图,当设备处于某个方向时,它们将被隐藏/显示。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
     // do some layout stuff
     NSLog(@"OverviewViewController shouldAutorotateToInterfaceOrientation");
     return YES;
}

每次我旋转我的设备时,这个方法都会执行两次,但是在任何子视图中它都不会被调用;

// In OverviewViewController

_landscapeView = [[LandscapeOverviewViewController alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:_landscapeView.view];


// In LandscapeOverviewViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
    NSLog(@"LandscapeOverviewViewController: shouldAutorateToInterfaceOrientation");

    // Only change orientation when it's landscape
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) return YES;
    return NO;
}

子视图不响应设备方向是否有原因?该方法从未在那里被调用过。

【问题讨论】:

  • 你是说,在旋转时,overviewViewController 调用 shouldAutorotateToInterfaceOrientation 方法,但 LandscapeOverviewViewController 不调用 shouldAutorotateToInterfaceOrientation ?
  • @calvinBhai 这正是我的意思,是的。
  • @Luke,谢谢你,但它没有解释如何解决它。
  • 该方法执行了两次,因为在 iOS5 上,旋转和呈现之间存在一些内部不一致。即它在旋转和演示之前被调用。在 iOS6 上,这已被更改。

标签: iphone orientation device


【解决方案1】:

正如我所见,您正在将 LandscapeOverviewViewController 视图作为子视图添加到 self.view
并且您希望您的 LandscapeOverviewViewControllershouldAutorotateToInterfaceOrientation 被调用,这永远不会发生。
因为这些被称为 pushedpresented 的控制器不像你正在做的那样。

解决方案: 你可以做两件事
1.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
     // do some layout stuff
     NSLog(@"OverviewViewController shouldAutorotateToInterfaceOrientation");
     //Call explicitly shouldAutorotateToInterfaceOrientation of LandscapeOverviewViewController
     [_landscapeView shouldAutorotateToInterfaceOrientation:interfaceOrientation];
     return YES;
}


2.您可以在LandscapeOverviewViewController注册方向通知

【讨论】:

  • 手动调用该方法可以正常工作,但这是一个好方法吗?
  • 一切正常,一切正常。但更好的方法是注册方向更改通知。我个人在我的一个项目中也做过同样的事情。
【解决方案2】:

您是否需要在您的overviewViewController 中使用两个单独的视图控制器(每个方向一个)?

如果您只是根据方向更改框架值,请确保使用自动调整大小蒙版。

如果没有,只需为每个方向使用单独的视图,并根据需要隐藏取消隐藏每个视图。

目前的问题是,您的 lanscapeViewController 没有人向它发送 viewControllerEvents。

在当前设置下,您可以在 OverviewViewController 中使用此代码以模态方式呈现这些视图控制器

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration{
{    
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        [self presentModalViewController:LandscapeOverviewViewController animated: NO];
        }
    else{
       [self presentModalViewController:PortraitOverviewViewController animated: NO];
       }
}

【讨论】:

  • 视图是布局的一部分,在设备旋转时完全不同,所以我不能将它们呈现为模态视图
  • 文档不鼓励在这种方法中进行这种处理。 shouldAutorotateToInterfactOrientation 应该只在适当的时候返回是/否。响应和更改应该在 didRotate.. 或其他相关协议中完成。
  • @DeanDavids 你是对的。可以使用 didRotate 或 willRotate 方法而不是在 shouldAutoRotate 中执行此操作。
  • 应用首次启动时触发 willRotateToInterfaceOrientation 的正确方法是什么?
  • 我不想手动触发它。它应该/将在设备旋转时以及 UI 发生任何更改之前调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
  • 2010-12-02
  • 1970-01-01
  • 2012-10-19
相关资源
最近更新 更多