纵向旋转后,UIViewController卡在水平方向

时间:2011-07-05 17:18:23

标签: iphone objective-c cocoa-touch uiview uiviewcontroller

View Controller A以水平方向显示View Controller B

#pragma mark Rotation Delegate Methods
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES;
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        [landscapeChartViewController.chartImageView reloadWithUrl:
            [NSString stringWithFormat:@"someurl",[symbol uppercaseString]]];

        NSLog(@"showing chart");
        [self presentModalViewController:landscapeChartViewController animated:NO];
    }    
}

这很好用。视图控制器B以横向显示。这是View Controller B的实现:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        NSLog(@"dismissing chart");
        [self.parentViewController dismissModalViewControllerAnimated:NO];
    }
}

问题是,当我回到纵向显示视图控制器A时,视图控制器A卡在横向方向。我该如何解决这个问题?

5 个答案:

答案 0 :(得分:0)

编辑:在阅读评论后,我建议您尝试使用willRotateToInterfaceOrientation:duration:代替willAnimateRotationToInterfaceOrientation,如下所示:

控制器A:

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
   if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
     [landscapeChartViewController.chartImageView reloadWithUrl:
        [NSString stringWithFormat:@"someurl",[symbol uppercaseString]]];

     NSLog(@"showing chart");
     [self presentModalViewController:landscapeChartViewController animated:NO];
   }    
}

控制器B:

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
      NSLog(@"dismissing chart");
      [self.parentViewController dismissModalViewControllerAnimated:NO];
  }
}

我在我的项目中或多或少地做了同样的事情,只在两个非模态视图之间。

答案 1 :(得分:0)

一种选择是将代码从willAnimateRotationToInterfaceOrientation:移至didRotateFromInterfaceOrientation:,并使用self.interfaceOrientation代替toInterfaceOrientation

答案 2 :(得分:0)

视图控制器B以横向显示。这是View Controller B的实现:

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    //为支持的方向返回YES 返回YES;

}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
    NSLog(@"dismissing chart");
    [self dismissModalViewControllerAnimated:NO];
}

}

答案 3 :(得分:0)

你实现了函数willRotateToInterfaceOrientation吗?还可以尝试使用通知中心通知父视图控制器您的模态视图控制器已旋转,然后只需[self dismissModalViewControllerAnimated:YES]

答案 4 :(得分:0)

我总是在didRotateFromInterfaceOrientation中编写我的方向逻辑。 这是我的代码的一部分,它工作得很好....

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
         [connectCoverLockUnlockSwitch setFrame:CGRectMake(250,6,51,31)];
         UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation ==    UIDeviceOrientationLandscapeRight){

            [connectCoverLockUnlockSwitch setFrame:CGRectMake(400,6,51,31)];
            [self.tableView reloadData];
        }
        else if (orientation == UIDeviceOrientationPortraitUpsideDown || orientation == UIDeviceOrientationPortrait){

        [connectCoverLockUnlockSwitch setFrame:CGRectMake(250,6,51,31)];

}         }

    else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)     {
          UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];        

       if (orientation == UIDeviceOrientationUnknown || orientation ==   UIDeviceOrientationFaceUp) {

//return;

  }
         if (orientation == UIDeviceOrientationLandscapeLeft || orientation ==   UIDeviceOrientationLandscapeRight) {
              [connectCoverLockUnlockSwitch setFrame:CGRectMake(570,6,51,31)];

               [self.tableView reloadData];
       }

  else if (orientation == UIDeviceOrientationPortraitUpsideDown || orientation ==   UIDeviceOrientationPortrait)
       {
            [connectCoverLockUnlockSwitch setFrame:CGRectMake(330,6,51,31)];
            [self.tableView reloadData];

          }
          }