Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-不同视图控制器的不同屏幕方向_Ios_Iphone_Objective C_Uinavigationcontroller_Uiinterfaceorientation - Fatal编程技术网

iOS-不同视图控制器的不同屏幕方向

iOS-不同视图控制器的不同屏幕方向,ios,iphone,objective-c,uinavigationcontroller,uiinterfaceorientation,Ios,Iphone,Objective C,Uinavigationcontroller,Uiinterfaceorientation,我知道这里有人问了很多,也讨论了很多,但我在过去的几天里一直在互联网上寻找不同的解决方案,无论我尝试什么,似乎都没有任何效果 编辑: 也试过了,还是没有运气 我有一个iOS7应用程序(我目前不关心对iOS6的支持,但拥有它会很好),它有一个根视图控制器,它有一个从侧面滑动的菜单(,具体地说),允许您在不同的屏幕之间切换。所选屏幕加载到导航控制器中,导航控制器和屏幕的视图控制器之间有一个“模态”类型的segue(第一个出现的屏幕除外,它与根视图控制器有关系)。我也尝试了“推”序列,同样的结果。 其

我知道这里有人问了很多,也讨论了很多,但我在过去的几天里一直在互联网上寻找不同的解决方案,无论我尝试什么,似乎都没有任何效果

编辑: 也试过了,还是没有运气

我有一个iOS7应用程序(我目前不关心对iOS6的支持,但拥有它会很好),它有一个根视图控制器,它有一个从侧面滑动的菜单(,具体地说),允许您在不同的屏幕之间切换。所选屏幕加载到导航控制器中,导航控制器和屏幕的视图控制器之间有一个“模态”类型的segue(第一个出现的屏幕除外,它与根视图控制器有关系)。我也尝试了“推”序列,同样的结果。 其中一个屏幕只能以横向模式显示,其他屏幕只能以纵向模式显示

为此,我在根视图控制器中实现了以下功能:

- (void)awakeFromNib
{
    self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"contentController"];
    self.menuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"menuController"];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.contentViewController)
        return [self.contentViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}
-(BOOL)shouldAutorotate
{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
在我的导航视图控制器中,我实现了以下功能:

- (void)awakeFromNib
{
    self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"contentController"];
    self.menuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"menuController"];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.contentViewController)
        return [self.contentViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}
-(BOOL)shouldAutorotate
{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
在每个纵向屏幕的视图控制器中,我都实现了:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
在横向屏幕中:

- (NSUInteger) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}
该应用程序的第一个屏幕是纵向屏幕。因此,所发生的是,应用程序在纵向模式下加载,并且不会旋转到任何其他方向(这很好)。但是加载横向屏幕后,它将以纵向模式加载,只有当我将设备旋转到横向模式时,它才会旋转并锁定到横向模式。一旦我切换回纵向屏幕,它将以横向模式加载,只有当我将设备旋转到纵向时,它才会旋转并锁定到纵向模式,并且由于某种原因,使屏幕非常窄

我所得到的最接近体面解决方案是在横向屏幕的视图控制器中实现这一点:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
    landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 0.0, 0.0);
    [self.navigationController.view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, screenRect.size.height, screenRect.size.width);
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
    landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 0.0, 0.0);
    [self.navigationController.view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, screenRect.size.width, screenRect.size.height);
}
#define degreesToRadian(x) (M_PI * (x)/180.0)
+(void)loadController:(UIViewController *)VControllerToLoad andRelease:(UIViewController *)VControllerToRelease
{
    //adjust the frame of the new controller
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
    CGRect windowFrame = [[UIScreen mainScreen] bounds];
    CGRect firstViewFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.size.height, windowFrame.size.width, windowFrame.size.height - statusBarFrame.size.height);
    VControllerToLoad.view.frame = firstViewFrame;
    //set the new controller as the root controller
    [[[UIApplication sharedApplication].delegate window] setRootViewController:VControllerToLoad];
    //kill the previous view controller
    [VControllerToRelease.view removeFromSuperview];
}
并将此视图控制器的支持方向和首选方向更改为纵向。这基本上使整个应用程序锁定在纵向模式。 虽然它看起来不错,但似乎有点粗略,我更希望有一个“干净”的解决方案,并支持左右两侧的景观。你知道我遗漏了什么吗

如果您需要我提供更多的代码,请告诉我。
谢谢!!:)

好吧,万一有人对此感兴趣,这是我的解决方案,使用公认的答案:

我缺少的是在我的方法中——横向VC不能与纵向VC处于同一根VC之下,它需要或者有自己的根VC,这是横向VC

首先,我在故事板中将景观VC与其他VC分开,现在它完全独立了。接下来,我创建了一个“view controller switch”方法,它基本上加载一个新的控制器,将其设置为根控制器,并释放以前的根控制器:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
    landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 0.0, 0.0);
    [self.navigationController.view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, screenRect.size.height, screenRect.size.width);
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
    landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 0.0, 0.0);
    [self.navigationController.view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, screenRect.size.width, screenRect.size.height);
}
#define degreesToRadian(x) (M_PI * (x)/180.0)
+(void)loadController:(UIViewController *)VControllerToLoad andRelease:(UIViewController *)VControllerToRelease
{
    //adjust the frame of the new controller
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
    CGRect windowFrame = [[UIScreen mainScreen] bounds];
    CGRect firstViewFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.size.height, windowFrame.size.width, windowFrame.size.height - statusBarFrame.size.height);
    VControllerToLoad.view.frame = firstViewFrame;
    //set the new controller as the root controller
    [[[UIApplication sharedApplication].delegate window] setRootViewController:VControllerToLoad];
    //kill the previous view controller
    [VControllerToRelease.view removeFromSuperview];
}
在横向VC中,我添加了以下代码:

-(BOOL)shouldAutorotate
{
    return YES;
}
每当我需要呈现横向VC或返回纵向VC时,我只使用VC切换方法。例如:

[AppUtils loadController:landscapeViewController andRelease:portraitNavigationController];
就这样!现在一切都像一个符咒一样工作了!:)