【问题标题】:'willRotateToInterfaceOrientation' not working for my DetailViewController'willRotateToInterfaceOrientation' 不适用于我的 DetailViewController
【发布时间】:2011-12-27 01:15:29
【问题描述】:

我正在 iPad 上开发此应用程序。我的 MainWindow 包含一个拆分视图控制器,它加载 RootViewController 和 DetailViewController。
我有这张图片位于 DetailViewController 的右下角。 我的应用程序允许不同的方向,因此我希望图像针对不同的方向出现在不同的位置。

这是我在 DetailViewController 中的代码:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
  {
      myImage.frame = CGRectMake(183.0f, 257.0f, 548.0f, 447.0f); 
  }
  else if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  {
      myImage.frame = CGRectMake(244.0f, 518.0f, 548.0f, 447.0f);
  }
}

当我启动我的应用程序并且我的 iPad 处于纵向时,图像将正确放置在右下角的 (X:244, Y:518) 处。 但是当我启动我的应用程序并且我的 iPad 处于横向时,图像将不会显示,我怀疑它仍然处于纵向位置。只有当我将 iPad 旋转到纵向,然后再转回横向时,图像才会出现在 (X:183, Y:257) 处,这是正确的。

我试图做的是将这些代码放在我的 DetailViewController 的“viewWillAppear”方法中:

- (void)viewWillAppear:(BOOL)animated 
{ 
  [super viewWillAppear:animated];
  if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
  {
      myImage.frame = CGRectMake(183.0f, 257.0f, 548.0f, 447.0f); 
  }
  else if(self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  {
      myImage.frame = CGRectMake(244.0f, 518.0f, 548.0f, 447.0f);
  }
}

但是当我启动我的应用程序时,它仍然有同样的问题。

我该怎么做才能在我第一次横向启动我的应用程序时,将其正确放置在 (X:244, Y:518) ?

【问题讨论】:

    标签: objective-c ios xcode ipad uiinterfaceorientation


    【解决方案1】:

    您可能想尝试将调整大小的逻辑放入didRotateFromInterfaceOrientation 而不是willRotateToInterfaceOrientation

    我的自定义布局代码仅在我在旋转完成后调用它时才起作用,而不是之前。

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
      NSLog(@"didRotateFromInterfaceOrientation");
    
      float width;
      float height;
    
      if ((fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || 
          (fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft))
      {
        width = [[UIScreen mainScreen] applicationFrame].size.width;
        height = [[UIScreen mainScreen] applicationFrame].size.height;
        NSLog(@"Changing to portrait: %f x %f", width, height);
      }
      else
      {
        width = [[UIScreen mainScreen] applicationFrame].size.height;
        height = [[UIScreen mainScreen] applicationFrame].size.width;
        NSLog(@"Changing to landscape: %f x %f", width, height);
      }
    
      NSLog(@"Changed orientation: %f x %f", width, height);
    
      // Call my custom layout function to setFrame on all my UI controls
      [self doLayout:width height:height];
    
      [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    }
    

    如果您的应用显示状态栏,那么您可能需要调用 self.view.frame.size.width 而不是从 applicationFrame 获取大小。

    【讨论】:

      【解决方案2】:

      确保您正在覆盖

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; // Override to allow rotation. Default returns YES only for UIInterfaceOrientationPortrait
      

      为您想要支持的每个 UIInterfaceOrentation 返回 YES,否则将不会调用 willRotateToInterfaceOrientation 和 didRotateToInterfaceOrientation。

      【讨论】:

      • 是的,我确实为此返回了“是”。当我尝试旋转我的 iPad 时,它会被调用,并且图像的位置确实发生了变化。问题是当我第一次以横向模式启动应用程序时,图像仍处于纵向模式时应位于的位置。我必须将我的 iPad 旋转为纵向,然后再转回横向,以使我的图像在横向模式下改变它的位置。
      【解决方案3】:

      尝试在您的 applicationDidFinishLauching 中尽早调用它:

      [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
      

      【讨论】:

        【解决方案4】:

        由于detailViewController 有时不知道它的方向,我通过在appDelegate 中有一个共享变量来解决这个问题,该变量将在整个应用程序中存储设备方向。您可以在 splitViewController 中使用它的方向对其进行初始化,这样当 detailViewController 加载时它可以从那里读取它。

        【讨论】:

        • 你也可以从 [UIApplication sharedApplication].statusBarOrientation 中读取方向
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-24
        • 2013-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多