簡體   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