xcode 'willRotateToInterfaceOrientation' 不适用于我的 DetailViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7762129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:07:05  来源:igfitidea点击:

'willRotateToInterfaceOrientation' not working for my DetailViewController

objective-ciosxcodeipaduiinterfaceorientation

提问by Lloydworth

I'm developing this application on the iPad. My MainWindow contains a Split View Controller which loads the RootViewController and DetailViewController.
I have this image that is placed at the bottom-right of the DetailViewController. My application allows different orientations, therefore i want the image to appear at different positions for different orientations.

我正在 iPad 上开发这个应用程序。我的 MainWindow 包含一个 Split View Controller,它加载 RootViewController 和 DetailViewController。
我将这张图片放置在 DetailViewController 的右下角。我的应用程序允许不同的方向,因此我希望图像出现在不同方向的不同位置。

This is my code in 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);
  }
}

When i launch my application and my iPad is at portrait orientation, the image will be correctly placed at (X:244, Y:518) which is at the bottom right. But when i launch my application and my iPad is at landscape orientation, the image will not be shown and i suspect that it is still at the position for portrait orientation. Only when i rotate my iPad to portrait orientation and then back to landscape orientation, then the image will appear at (X:183, Y:257) which is correct.

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

What i tried to do is to place these codes in my DetailViewController's 'viewWillAppear' method:

我试图做的是将这些代码放在我的 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);
  }
}

But when i launch my application, it still has the same problem.

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

What can i do so that when i first launch my application in landscape orientation, it will be correctly placed at (X:244, Y:518) ?

我该怎么做才能让我第一次以横向模式启动我的应用程序时,它会被正确地放置在 (X:244, Y:518) ?

回答by Dan J

You might want to try putting your resizing logic in didRotateFromInterfaceOrientationinstead of willRotateToInterfaceOrientation.

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

My custom layout code only worked when I called it after the rotation had completed it, rather than before.

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

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

If your app shows the status bar then you probably need to call self.view.frame.size.widthinstead of getting the size from the applicationFrame.

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

回答by Scott McCoy

Make sure that you're overriding

确保你覆盖

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

To return YES for each UIInterfaceOrentation that you want to support, otherwise willRotateToInterfaceOrientation and didRotateToInterfaceOrientation wont get called.

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

回答by Scott McCoy

Try calling this early in your applicationDidFinishLauching:

尝试在您的 applicationDidFinishLauching 早期调用它:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

回答by Fran Sevillano

Since the detailViewControllerwouldn't sometimes know its orientation, I solved this issue by having a shared variable in the appDelegatethat would store the device orientation throughout the app. You can initialize it in the splitViewControllerwith its orientation so when the detailViewControllerloads it can read it from there.

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