繁体   English   中英

仅一个视图控制器中的横向和纵向方向

[英]Landscape and portrait orientations in only one viewcontroller

在我的应用程序中,我正在使用MPMoviePlayerController 我需要同时以横向和纵向显示电影播放器​​。 我只希望movie player以这种方式显示,而其他屏幕则只能以纵向显示。 我被困住了,没有解决办法。 通过设置,我检查了以下三种模式。

我的项目设置

我正在使用ios 7和xcode5。谢谢

在包含MPMoviePlayerController的ViewController中,实现此功能(iOS6及更高版本):

- (NSUInteger)supportedInterfaceOrientations
{
  return UIInterfaceOrientationMaskAll; //allow rotate landscape, portrait
}

在其他viewControllers中:

- (NSUInteger)supportedInterfaceOrientations
{ 
  return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown; // portrait only 
}

对于iOS版本低于6,实现此方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
- (BOOL)shouldAutorotate

请注意:包含以上VC的ViewController应该实现以上方法

创建NavigationController的继承的类

BaseNavigationController.h

进口

@interface BaseNavigationController:UINavigationController

@结束

BaseNavigationController.m

实用标记-方向更改处理

-(BOOL)应该AutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL应该是AutorotateBool = NO;

if ([self isRotate])
   {
    if([[self.viewControllers lastObject] respondsToSelector:@selector(shouldAutorotate)])
        shouldAutorotateBool =  [[self.viewControllers lastObject] shouldAutorotate];
}
return shouldAutorotateBool;

}

// iOS6 / 7支持-(NSUInteger)支持InterfaceOrientations {

NSUInteger interfaceOrientation = UIInterfaceOrientationMaskPortrait;

if ([self isRotate]) {
    if([[self.viewControllers lastObject] respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)]) {
        interfaceOrientation = [[self.viewControllers lastObject]  preferredInterfaceOrientationForPresentation];
    }
}
return interfaceOrientation;

}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationPortrait;

if ([self isRotate]) {
    if([[self.viewControllers lastObject] respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)]) {
        interfaceOrientation = [[self.viewControllers lastObject]  preferredInterfaceOrientationForPresentation];
    }
}
return interfaceOrientation;

}

-(BOOL)应该自动旋转{

BOOL shouldAutorotateBool = NO;
if ([self isRotate])

{if([[[self.viewControllers lastObject] respondsToSelector:@selector(shouldAutorotate)])shouldAutorotateBool = [[[self.viewControllers lastObject] shouldAutorotate]; }

return shouldAutorotateBool;

}

-(BOOL)isRotate {如果([[[self.viewControllers lastObject] isMemberOfClass:NSClassFromString(@“ VideoVC”)]){

    return YES;

}
return NO;

}

**并且特定的viewController使用以下代码进行定向**

实用标记-设备方向处理方法

-(BOOL)应该AutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

return YES;

}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

return UIInterfaceOrientationLandscapeLeft;

}

-(NSUInteger)支持的接口方向{

return UIInterfaceOrientationMaskLandscapeLeft;

}

-(BOOL)应该自动旋转{

return YES;

}

仅更改MoviePlayer的最佳方法

    - (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }
}

首先,如果要为iOS6开发应用程序,则应使用UIViewController文档中介绍的iOS6方法。 像取向方法shouldAutorotateToInterfaceOrientation在iOS6的已过时,对iOS6的另一种方法是shouldAutoRotate 如果您的应用还支持iOS5,则应仅使用旧方法。

其次,如果您在应用程序中使用UINavigationcontroller且需要具有不同的界面方向,则navigationController可能会弄乱应用程序中的界面方向。 可能的解决方案(对我有用)是实现一个自定义UINavigationController并重写该自定义UINavigationController类中的界面方向方法,这将使您的viewControllers根据您设置的方向旋转,因为您的控制器是从UINavigationController推入的。 不要忘记在您的特定viewController中添加这些方法。

CustomNavigationController.h

@interface CustomNavigationController : UINavigationController
@end

CustomNavigationController.m

@implementation CustomNavigationController

//overriding shouldRotate method for working in navController
-(BOOL)shouldAutorotate
{  
  return   [self.topViewController shouldAutorotate];   
}

-(NSUInteger)supportedInterfaceOrientations
{
   return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return [self.topViewController preferredInterfaceOrientationForPresentation];
}

暂无
暂无

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

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