繁体   English   中英

按下后退按钮时更改界面方向

[英]Change interface orientation when back button is pressed

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

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

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

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

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

更新:

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

-(BOOL)shouldAutorotate
{
    return YES;
}

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

现在,如果我使用“播放” button从视图中按以下顺序进行操作。

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

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

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

此答案上找到了解决方案。

单击按钮时,我添加了一个通知,然后单击完成按钮时,我使用以下代码更改了方向。 我将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"];
}

据您对我的问题的了解,我已经为您的问题提供了可行的配置。

您已经需要一个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;
}

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

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

希望这会有所帮助

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM