0

![Second View its main gallery view][1] 你好,我正在尝试在设备处于横向时调用一个新的视图控制器。这是一个带有滚动视图的图片库,问题是我想在不同的位置显示图片库,但我不知道如何。我正在使用带有故事板的 Xcode 4.3,所以它与我认为的旧版本有点不同。

感谢您的帮助。![这是没有横向代码的外观,滚动消失] [2]这就是我调用另一个视图的原因

当!我不允许发布图片

4

2 回答 2

1

在情节提要中制作横向视图。在检查器中添加一个标识符,如 LandscapeView。然后,在你原来的视图控制器中,导入横向视图的视图控制器的头文件:

#import "LandscapeViewController.h" //Change to whatever your file is called

并像这样修改以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    LandscapeViewController *landscapeView = [self.storyboard instantiateViewControllerWithIdentifier:@"LandscapeView"]; //change LandscapeViewController to the correct name and change LandscapeView to the correct identifier
    landscapeView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; //Or whatever transition you want

    [self presentModalViewController:landscapeView animated:NO];
    return YES;
}

然后,在横屏视图的视图控制器中,修改同样的方法如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [self dismissModalViewControllerAnimated:NO];
    return YES;
}
于 2012-06-07T18:29:02.633 回答
0

您可以使用以下方法检测方向:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

然后您可以编写一个函数,以便在检测到更改时继续使用新的 ViewController:

-(void) detectOrientation {

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
    {
        if(!self->wasInLandscape)
        {
            [self performSegueWithIdentifier:@"Landscape Segue" sender:self];
            self->wasInLandscape = YES;
        }


    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    {

        if(self->wasInLandscape)
        {
            [self dismissModalViewControllerAnimated:NO];
            self->wasInLandscape = NO;
        }

    }   
}

有了这个,我将 segue 设置为没有动画的模态 segue,因此它只会在您切换时呈现出来。当wasInLandscape BOOL我们已经在当前位置时,用于确保我们不会解雇或执行 segues。

于 2012-06-07T18:27:56.623 回答