【问题标题】:UIViewController gets stuck in horizontal orientation after portrait rotationUIViewController 在纵向旋转后卡在水平方向
【发布时间】:2011-07-05 17:18:23
【问题描述】:

视图控制器 A 以水平方向显示视图控制器 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 卡在横向。我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    编辑:阅读您的评论后,我建议尝试使用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];
      }
    }
    

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

    【讨论】:

    • 你正确理解了我的问题。不幸的是,当我实现此代码时,视图控制器 B 在旋转到纵向模式时不会被关闭。
    • 这也不能正常工作。旋转到纵向模式时,视图控制器 B 不会被关闭。
    • 您是否已将原来的shouldAutorotateToInterfaceOrientation 实施放回原处,总是返回YES?
    • 是的,我尝试了两种总是返回 YES 的方案,然后是您之前回答的建议。都不行。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

        // 对于支持的方向返回 YES 返回是;

      }

      -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 持续时间:(NSTimeInterval)duration {

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

      }

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          我总是在 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];
          
                    }
                    }
          

          【讨论】:

            猜你喜欢
            • 2013-07-02
            • 1970-01-01
            • 2011-03-06
            • 1970-01-01
            • 1970-01-01
            • 2012-03-06
            • 1970-01-01
            • 1970-01-01
            • 2012-12-27
            相关资源
            最近更新 更多