我的应用只接受纵向导视,MPMoviePlayerController
除外,我想让用户更改方向。
因此,我将UINavigationController
子类化,并添加了以下代码:
-(BOOL)shouldAutorotate
{
if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return YES;
}
else
{
return NO;
}
}
效果很好,只允许MPMoviePlayerViewController
更改方向。问题是当用户处于横向并按下done
按钮或播放结束时,它会弹出moviePlayer
并返回到presentationViewController,但是在横向模式下会导致崩溃,因为该视图不适合横向。
我尝试了一些事情来改回Portrait
但没有运气。我使用故事板,如果这有任何区别。我想在viewWillAppear
上将方向更改回纵向,或者按下done
按钮并更改其方向。
更新:
以下是我的UINavigationController
子类中的更新代码:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
现在,如果我从播放button
的视图中按以下顺序执行操作。
MPMoviePlayerController
类)button
。 (它表示播放器已经处于横向模式)。现在,如果我将订单更改为:
答案 0 :(得分:4)
在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"];
}
答案 1 :(得分:0)
就我的问题而言,我的问题是有效的。
您需要一个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;
}
它对我有用,希望它适合你。
答案 2 :(得分:-1)
-(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];
}
希望这会有所帮助