I want to only support vertical orientation throughout all the view controllers of my iOS app. However, I embed a YouTube video in one of my view controllers, and when that video is selected to take up the full screen, I want the user to be able to orient his/her phone horizontally so the video expands to take the full screen.
我想只在我的iOS应用程序的所有视图控制器中支持垂直方向。但是,我在我的一个视图控制器中嵌入了一个YouTube视频,当选择该视频占据整个屏幕时,我希望用户能够水平定位他/她的手机,以便视频扩展为全屏。
EDIT: I tried using the following code from Autorotate in iOS 6 has strange behaviour:
编辑:我尝试在iOS 6中使用Autorotate的以下代码有奇怪的行为:
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return self.fullScreenVideoIsPlaying ?
UIInterfaceOrientationMaskAllButUpsideDown :
UIInterfaceOrientationMaskPortrait;
}
and in my view controller that presents the UIWebView
/YouTube frame, I have this code in my viewDidLoad
:
在我的视图控制器中显示UIWebView / YouTube框架,我在viewDidLoad中有这个代码:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(windowNowVisible:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window
];
- (void)windowNowVisible:(NSNotification *)notification
{
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.fullScreenVideoIsPlaying = !(appDelegate.fullScreenVideoIsPlaying);
}
However, when the user presses done on the fullscreen YouTube video, if he/she still has the phone horizontally, then the presenting view controller also stays horizontal (I want the present view controller to be portrait). It's a race on the fullSreenVideoIsPlaying
variable which isn't updating fast enough so that my presenting view controller is portrait.
然而,当用户在全屏YouTube视频上按下完成时,如果他/她仍然水平地具有电话,则呈现视图控制器也保持水平(我希望当前视图控制器是肖像)。这是fullSreenVideoIsPlaying变量的竞赛,它没有足够快地更新,因此我的呈现视图控制器是纵向的。
Any feedback on how to achieve this would be greatly appreciated.
任何有关如何实现这一目标的反馈将不胜感激。
Thanks!
2 个解决方案
#1
21
Figured out a solution by molding together a few solutions I've found on *.
通过将我在*上找到的一些解决方案组合在一起来找出解决方案。
Instead of using this notification
而不是使用此通知
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(windowNowVisible:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window
];
I use the following notifications
我使用以下通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
with the implementations
与实现
-(void) youTubeStarted:(NSNotification*) notif {
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.fullScreenVideoIsPlaying = YES;
}
-(void) youTubeFinished:(NSNotification*) notif {
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.fullScreenVideoIsPlaying = NO;
}
and in my AppDelegate, I have
在我的AppDelegate中,我有
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if (self.fullScreenVideoIsPlaying) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else {
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
and in my view controllers, I have
在我看来,控制器,我有
-(BOOL) shouldAutorotate {
return NO;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
#2
6
I've encountered the very same problem before, and the best solution I could find that worked under iOS 5.x and 6.x was to present the video controller in a modal view.
我以前遇到过同样的问题,我能找到的最好的解决方案是在iOS 5.x和6.x下工作的,它是以模态视图呈现视频控制器。
The modal view is a UINavigationController
that wraps the video controller and responds with UIInterfaceOrientationMaskAll
in supportedInterfaceOrientations
. In the modal view's viewWillAppear:
, I flip fullScreenVideoIsPlaying
to YES
and set it to NO
in viewWillDisappear:
. This arrangement will keep the underlying view controllers in portrait while allowing the modal view to rotate as the user sees fit.
模态视图是一个UINavigationController,它包装视频控制器并使用supportedInterfaceOrientations中的UIInterfaceOrientationMaskAll进行响应。在模态视图的viewWillAppear:中,我将fullScreenVideoIsPlaying翻转为YES并在viewWillDisappear中将其设置为NO:这种安排将使底层视图控制器保持纵向,同时允许模态视图在用户认为合适时旋转。
#1
21
Figured out a solution by molding together a few solutions I've found on *.
通过将我在*上找到的一些解决方案组合在一起来找出解决方案。
Instead of using this notification
而不是使用此通知
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(windowNowVisible:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window
];
I use the following notifications
我使用以下通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
with the implementations
与实现
-(void) youTubeStarted:(NSNotification*) notif {
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.fullScreenVideoIsPlaying = YES;
}
-(void) youTubeFinished:(NSNotification*) notif {
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.fullScreenVideoIsPlaying = NO;
}
and in my AppDelegate, I have
在我的AppDelegate中,我有
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if (self.fullScreenVideoIsPlaying) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else {
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
and in my view controllers, I have
在我看来,控制器,我有
-(BOOL) shouldAutorotate {
return NO;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
#2
6
I've encountered the very same problem before, and the best solution I could find that worked under iOS 5.x and 6.x was to present the video controller in a modal view.
我以前遇到过同样的问题,我能找到的最好的解决方案是在iOS 5.x和6.x下工作的,它是以模态视图呈现视频控制器。
The modal view is a UINavigationController
that wraps the video controller and responds with UIInterfaceOrientationMaskAll
in supportedInterfaceOrientations
. In the modal view's viewWillAppear:
, I flip fullScreenVideoIsPlaying
to YES
and set it to NO
in viewWillDisappear:
. This arrangement will keep the underlying view controllers in portrait while allowing the modal view to rotate as the user sees fit.
模态视图是一个UINavigationController,它包装视频控制器并使用supportedInterfaceOrientations中的UIInterfaceOrientationMaskAll进行响应。在模态视图的viewWillAppear:中,我将fullScreenVideoIsPlaying翻转为YES并在viewWillDisappear中将其设置为NO:这种安排将使底层视图控制器保持纵向,同时允许模态视图在用户认为合适时旋转。