[英]How to change interface orientation from landscape to portrait when back button is tapped in Objective C
[英]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
从视图中按以下顺序进行操作。
MPMoviePlayerController
类) button
。 (它显示播放器已经处于横向模式)。 现在,如果我将顺序更改为:
在此答案上找到了解决方案。
单击按钮时,我添加了一个通知,然后单击完成按钮时,我使用以下代码更改了方向。 我将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.