简体   繁体   中英

Landscape and portrait orientations in only one viewcontroller

In my app I am using MPMoviePlayerController . I need to display movie player in both landscape and portrait orientations. I only want movie player to display like this and other screens should only be portrait oriented. I am stuck and getting no solutions. From settings I've checked three modes as shown below.

我的项目设置

I am using ios 7 and xcode 5. Thanks

In your ViewController which contains MPMoviePlayerController, implement this (iOS6 and above):

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

In other viewControllers:

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

To iOS version is lower than 6, implement this method:

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

Note that: ViewController which contain above VCs should implement above methods

Create Inherited class of NavigationController

BaseNavigationController.h

import

@interface BaseNavigationController : UINavigationController

@end

BaseNavigationController.m

pragma mark- Orientation changes handling

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL shouldAutorotateBool = NO;

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

}

// iOS6/7 support - (NSUInteger) supportedInterfaceOrientations {

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)shouldAutorotate {

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

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

return shouldAutorotateBool;

}

-(BOOL) isRotate { if ([[self.viewControllers lastObject] isMemberOfClass:NSClassFromString(@"VideoVC")]) {

    return YES;

}
return NO;

}

**And the particular viewController use the following Code for orientation**

pragma mark- Methods for device orientation handling

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

return YES;

}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

return UIInterfaceOrientationLandscapeLeft;

}

-(NSUInteger)supportedInterfaceOrientations{

return UIInterfaceOrientationMaskLandscapeLeft;

}

-(BOOL)shouldAutorotate {

return YES;

}

Best way to change orientation only for the MoviePlayer

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

First you should use methods for the iOS6 presented in UIViewController documentation if you are making your app for iOS6. Orientation method like shouldAutorotateToInterfaceOrientation is deprecated in iOS6, alternate method for iOS6 is shouldAutoRotate . You should only use the old method if your app is supporting also iOS5.

Second If you are using UINavigationcontroller in your application and you need to have different interface orientations then navigationController could mess up the interface orientation in the application. Possible solution (worked for me) is to implement a custom UINavigationController and override the interface orientation methods within that custom UINavigationController class, this will make your viewControllers rotate according to the orientation you set because your controllers are pushed from the UINavigationController . Don't forget to add those methods in your particular viewController also.

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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