按下后退按钮时更改界面方向 [英] Change interface orientation when back button is pressed

查看:61
本文介绍了按下后退按钮时更改界面方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了 MPMoviePlayerController (我想允许用户更改方向)之外,我的应用仅接受纵向方向.

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

 -(BOOL)应该自动旋转{如果([[[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController类]]){返回是;}别的{返回否;}} 

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

我尝试了几件事改回 Portrait ,但是没有运气.我正在使用情节提要,如果有什么不同的话.我想在 viewWillAppear 上将方向更改回纵向,或者也许要按 done 按钮并在那里更改方向.

更新:

这是我的 UINavigationController 子类中的更新代码:

 -(BOOL)应该自动旋转{返回是;}-(NSUInteger)支持的接口方向{如果([[[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController类]]){返回UIInterfaceOrientationMaskAll;}别的{返回UIInterfaceOrientationMaskPortrait;}} 

现在,如果我使用播放" 按钮从视图中按以下顺序进行操作.

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

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

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

解决方案

此答案上找到了解决方案./p>

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

 -(IBAction)playButton:(id)sender {[[NSNotificationCenter defaultCenter] addObserver:self选择器:@选择器(moviePlaybackDidFinish)名称:MPMoviePlayerPlaybackDidFinishNotificationobject:self.player.movi​​ePlayer];NSURL * url = [NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/crzu6yrwt35tgej/flexao.mp4"];self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];self.player.movi​​ePlayer.movi​​eSourceType = MPMovieSourceTypeFile;[self presentMoviePlayerViewControllerAnimated:self.player];//presentMoviePlayerViewControllerAnimated:self.player];}-(void)moviePlaybackDidFinish{[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait]forKey:@"orientation"];} 

My app accepts only portrait orientation, except for MPMoviePlayerController, which I want to allow user to change orientation.

So, I subclassed UINavigationController and added the following code:

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

It works great, allowing only my MPMoviePlayerViewController to change orientation. The problem is that when user is in landscape orientation and presses the done button or playback ends, it pops the moviePlayer and goes back to the presentingViewController, but on landscape mode, causing a crash, since that view is not made for landscape orientation.

I tried a few things to change back to Portrait but had no luck. I'm using storyboards, if that makes any difference. I would like to change the orientation back to portrait on viewWillAppear, or maybe getting the donebutton press and change the orientation there.

UPDATE:

Here is the updated code in my UINavigationControllersubclass:

-(BOOL)shouldAutorotate
{
    return YES;
}

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

Now, if I do things in the following order from the view with the Play button.

  1. Rotate the device. (it calls the method but screen doesn't rotate since it is not MPMoviePlayerController class)
  2. Press the play button. (It presents the player already on landscape mode).
  3. Press the back button. (It pops the player and correctly shows the view on portrait mode)

Now, if I change the order to:

  1. Press the play button. (holding the device on the regular portrait position).
  2. Rotate the device. (it rotates the movie player correctly showing the video).
  3. Press the back button. (it pops the player, but this time, the view is in landscape mode, which is not the expected behavior)

解决方案

Found a solution here on this answer.

On button click, I added a notification and then on done button click I used the below code to change orientation. I kept the UINavigationController subclass as in my question to allow the change of orientation when movie starts playing.

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

这篇关于按下后退按钮时更改界面方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆