简体   繁体   English

'willRotateToInterfaceOrientation'不能用于我的DetailViewController

[英]'willRotateToInterfaceOrientation' not working for my DetailViewController

I'm developing this application on the iPad. 我正在iPad上开发此应用程序。 My MainWindow contains a Split View Controller which loads the RootViewController and DetailViewController. 我的MainWindow包含一个拆分视图控制器,该控制器加载RootViewController和DetailViewController。
I have this image that is placed at the bottom-right of the DetailViewController. 我有这个图像放在DetailViewController的右下角。 My application allows different orientations, therefore i want the image to appear at different positions for different orientations. 我的应用程序允许不同的方向,因此我希望图像针对不同的方向出现在不同的位置。

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. 当我启动我的应用程序并且我的iPad处于纵向时,图像将被正确放置在右下方的(X:244,Y:518)。 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. 但是当我启动我的应用程序并且我的iPad处于横向时,图像将不会显示,我怀疑它仍处于纵向定位的位置。 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: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)?

You might want to try putting your resizing logic in didRotateFromInterfaceOrientation instead 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.width instead of getting the size from the applicationFrame . 如果您的应用显示状态栏,那么您可能需要调用self.view.frame.size.width而不是从applicationFrame获取大小。

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。

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

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Since the detailViewController wouldn't sometimes know its orientation, I solved this issue by having a shared variable in the appDelegate that would store the device orientation throughout the app. 由于detailViewController有时不知道它的方向,我通过在appDelegate中使用一个共享变量来解决这个问题,该变量将在整个应用程序中存储设备方向。 You can initialize it in the splitViewController with its orientation so when the detailViewController loads it can read it from there. 您可以在splitViewController中以其方向初始化它,因此当detailViewController加载时,它可以从那里读取它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 willRotateToInterfaceOrientation在Universal MasterDetail应用中不起作用 - willRotateToInterfaceOrientation not working in Universal MasterDetail app 将数据发送到detailViewController无法正常工作? - Sending data to detailViewController not working? 如果在我的detailView(splitView)内部,如何将位置设置为UILabel,但是在viewDidLoad和willRotateToInterfaceOrientation中遇到问题 - How to set the position to UILabel if is inside my detailView (splitView) but I have trouble in the viewDidLoad and willRotateToInterfaceOrientation willRotateToInterfaceOrientation没有被调用 - willRotateToInterfaceOrientation not getting called 手动路由willRotateToInterfaceOrientation调用 - Manually route willRotateToInterfaceOrientation calls 在DetailViewController中的subView? - subView in DetailViewController? 在DetailViewController上,倒计时 - On DetailViewController,countdown 我的DetailViewController有文本字段。 在我第一次点击后它似乎是空的 - my DetailViewController has text field. It appears empty at my first tap after that 替换我的UISplitView中的detailViewController会在旋转我的应用程序Objective-C时崩溃 - replacement of my detailViewController in UISplitView crashes on rotating my app Objective-C 以编程方式从另一个DetailViewController中的表中推送一个DetailViewController - pushing a DetailViewController from a table in another DetailViewController programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM