处理Ios7中针对特定ViewControllers的方向

时间:2014-07-16 06:43:28

标签: ios xcode orientation

我正在创建一个电子书应用程序,其中我只给出了两个ViewControllers的所有方向。

我成功地将方向设置为两个ViewControllers并首次执行“旋转”。但后来当我使用新书加载视图控制器时,旋转无法正常工作。

我使用了这些方法:

  -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                           duration:(NSTimeInterval)duration{
  if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )    {
  //code for landscape
  }
  else{
   //code for protrait.
  }
  }

  - (BOOL)shouldAutorotate{
    return YES;
  } 
 - (NSUInteger)supportedInterfaceOrientations{
    return (UIInterfaceOrientationMaskAll);
 }

2 个答案:

答案 0 :(得分:0)

尝试在UINavigationControllers上处理旋转,UINavigationController子类上的确切代码是正确的。并确保将您的UIViewControllers设置为新UINavigationControllers的childViewControllers。 请记住,您需要使用键"支持的界面方向设置支持的方向"在你的info.plist。

答案 1 :(得分:0)

创建UINavigationController的子类

@interface BaseNavigationController:UINavigationController

@end

@implementation BaseNavigationController

    - (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;
    }
@end

在那个特定的viewController中:

#import "VideoVC.h"

@interface VideoVC ()

@end

@implementation VideoVC
#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;
}

@end