【问题标题】:Change interface orientation when back button is pressed按下后退按钮时更改界面方向
【发布时间】:2014-09-09 14:26:42
【问题描述】:

我的应用只接受纵向,MPMoviePlayerController 除外,我想允许用户更改方向。

所以,我将UINavigationController 子类化并添加了以下代码:

-(BOOL)shouldAutorotate
{
    if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

效果很好,只允许我的MPMoviePlayerViewController 改变方向。问题是当用户处于横向并按下done 按钮或播放结束时,它会弹出moviePlayer 并返回到presentingViewController,但在横向模式下,会导致崩溃,因为该视图不是为横向。

我尝试了几件事来改回Portrait,但没有运气。我正在使用故事板,如果这有什么不同的话。我想在viewWillAppear 上将方向改回纵向,或者按下done 按钮并在那里更改方向。

更新:

这是我的UINavigationControllersubclass 中的更新代码:

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
    {
        return UIInterfaceOrientationMaskAll;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }
}

现在,如果我在 Play button 的视图中按以下顺序执行操作。

  1. 旋转设备。 (它调用该方法但屏幕不旋转,因为它不是MPMoviePlayerController 类)
  2. 按播放button。 (它显示玩家已经处于横向模式)。
  3. 按返回按钮。 (它会弹出播放器并正确显示纵向模式的视图)

现在,如果我将顺序更改为:

  1. 按下播放按钮。 (将设备保持在常规纵向位置)。
  2. 旋转设备。 (它会旋转电影播放器​​以正确显示视频)。
  3. 按返回按钮。 (它会弹出播放器,但这一次,视图处于横向模式,这不是预期的行为)

【问题讨论】:

    标签: ios objective-c iphone screen-orientation uiinterfaceorientation


    【解决方案1】:

    this answer 上找到了解决方案。

    单击按钮时,我添加了一个通知,然后单击完成按钮时,我使用以下代码更改方向。我在我的问题中保留了UINavigationController 子类,以允许在电影开始播放时改变方向。

    - (IBAction)playButton:(id)sender {        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlaybackDidFinish)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.player.moviePlayer];
    
        NSURL *url = [NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/crzu6yrwt35tgej/flexao.mp4"];        
        self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
        self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        [self presentMoviePlayerViewControllerAnimated:self.player]; // presentMoviePlayerViewControllerAnimated:self.player];
    }
    
    -(void)moviePlaybackDidFinish
    {
        [[UIDevice currentDevice] setValue:
         [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                    forKey:@"orientation"];
    }
    

    【讨论】:

      【解决方案2】:

      据我从你的问题中了解到,我有一个可以解决你的问题的有效配置。

      您需要一个 UINavigationController 的子类,就像您已经拥有的一样,但使用以下代码:

      .h 文件:

      @interface EECNavigationController : UINavigationController
      
      @property (assign, nonatomic) BOOL landscapeDisallowed;
      
      @end
      

      .m 文件:

      #import "EECNavigationController.h"
      
      @implementation EECNavigationController
      
      - (BOOL)shouldAutorotate
      {
          return YES;
      }
      
      - (NSUInteger)supportedInterfaceOrientations
      {
          if( !_landscapeDisallowed ) {
              // for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
              return UIInterfaceOrientationMaskAll;
          }
          return UIInterfaceOrientationMaskPortrait;
      }
      
      @end
      

      现在,在此导航控制器下的视图控制器中,您只需在 viewDidLoad 方法中将“landscapeDisallowed”设置为 YES,例如:

      - (void)viewDidLoad {
          [super viewDidLoad];
      
          ((EECNavigationController *)self.navigationController).landscapeDisallowed = YES;
      }
      

      并像这样覆盖以下方法:

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
          return toInterfaceOrientation == UIInterfaceOrientationPortrait;
      }
      
      - (NSUInteger)supportedInterfaceOrientations {
          return UIInterfaceOrientationMaskPortrait;
      }
      

      可以在横向模式下呈现的视图只需要:

      - (void)viewDidLoad {
          [super viewDidLoad];
      
          ((EECNavigationController *)self.navigationController).landscapeDisallowed = NO;
      }
      

      它对我有用,希望它对你有用。

      【讨论】:

      • 谢谢,这是一个很好的代码,但它不能解决我的问题。它正确定义了每个方向允许哪些视图,但是,当我转到 landscapeDisallowed = NO 的视图时,更改方向并以横向模式弹出该视图,它会以横向模式显示前一个视图,即使是 landscapeDisallowed = YES; .预期的行为是让它手动将方向切换为纵向。
      • 似乎对此有更好的回应:stackoverflow.com/questions/12662240/…
      【解决方案3】:
      -(void)viewWillAppear:(BOOL)animated
      {
      
      
             [self updateLayoutForNewOrientation:self.interfaceOrientation];
      }
      - (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation
      {
          if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
          {
              // Portrait view
      
              if(iPhone5)
      
              {
              }else{
      
              }
      
              if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
              {
      
              }
      
      
          }
      
          else
          {
              // Landscape view
      
              if(iPhone5)
      
              {
      
      
              }else{
                  float SystemVersion=[[[UIDevice currentDevice] systemVersion] floatValue];
                  if(SystemVersion>=7.0f)
                  {
      
      
                  }else{
      
                  }
              }
      
              if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
              {
                  if(iPhone5)
                  {
      
                  }
                  else
                  {
      
                  }
              }
      
      
          }
      }
      
      - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
      {
          [self updateLayoutForNewOrientation:self.interfaceOrientation];
      }
      

      希望这会有所帮助

      【讨论】:

      • 好吧,这段代码获取了当前的方向,但实际上并没有将方向改回纵向,对吧?我不想根据方向更新我的布局,但确实改变了方向以适合我的布局。
      猜你喜欢
      • 2015-03-24
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多