带有自定义segue的iOS容器视图 - 维护子容器的容器大小

时间:2015-05-28 05:37:00

标签: ios objective-c iphone uistoryboardsegue uicontainerview

我正在创建一个与用户进行“会话”的应用程序,它会询问他们的问题并根据他们的回答将他们引导到故事板中的不同屏幕。我的解决方案松散地基于这篇博文

http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers

但是我不是在两个孩子之间切换,而是从第一个开始并将每个新问题屏幕添加到子控制器堆栈(我以编程方式添加按钮以转到下一个问题和上一个问题),因为用户将需要该选项在他们参加会议期间回去改变他们的答案。根据博客文章,我使用了一个具有空执行方法的自定义segue

- (void)perform
{
    // Nothing. The ContainerViewController class handles all of the view
    // controller action.
}

我在父

中实现了切换逻辑
// Sets up the segue identifier and tells swapFromViewController: toViewController:
// function what view controllers to perform the segue one
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
     // Instead of creating new VCs on each seque we want to hang on to existing
     // instances if we have it.
     if ([segue.identifier isEqualToString:SegueIdentifierFirst]) {
         self.firstViewController = segue.destinationViewController;

         // If this is the very first time we're loading this we need to do
         // an initial load.
         [self addChildViewController:segue.destinationViewController];
         UIView* destView = ((UIViewController *)segue.destinationViewController).view;
         destView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
         destView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

         [self.view addSubview:destView];
         [segue.destinationViewController didMoveToParentViewController:self];
     }
     else if ([segue.identifier isEqualToString:SegueIdentifierSecond]) {
         self.secondViewController = segue.destinationViewController;
         [self swapFromViewController:[self.childViewControllers objectAtIndex:self.currentChildVCIndex] toViewController:self.secondViewController];
     }
     else if ([segue.identifier isEqualToString:SegueIdentifierThird]) {
         self.thirdViewController = segue.destinationViewController;
         [self swapFromViewController:[self.childViewControllers objectAtIndex:self.currentChildVCIndex] toViewController:self.thirdViewController];
     }
     else if ([segue.identifier isEqualToString:SegueIdentifierFourth]) {
         self.fourthViewController = segue.destinationViewController;
         [self swapFromViewController:[self.childViewControllers objectAtIndex:self.currentChildVCIndex] toViewController:self.fourthViewController];
     }
     else if ([segue.identifier isEqualToString:SegueIdentifierFifth]) {
         self.fifthViewController = segue.destinationViewController;
         [self swapFromViewController:[self.childViewControllers objectAtIndex:self.currentChildVCIndex] toViewController:self.fifthViewController];
     }
     else if ([segue.identifier isEqualToString:SegueIdentifierSixth]) {
         self.sixthViewController = segue.destinationViewController;
         [self swapFromViewController:[self.childViewControllers objectAtIndex:self.currentChildVCIndex] toViewController:self.sixthViewController];
     }
}

交换方法

// Given two view controllers, transition from one to the other and add to the child controller stack so
// that we can maintain navigation
- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
    toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [fromViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];

    [self transitionFromViewController:fromViewController toViewController:toViewController duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        self.currentChildController = toViewController;
        [toViewController didMoveToParentViewController:self];
        self.transitionInProgress = NO;
    }];
}

按下下一个按钮时

// When the next question button is pressed in the view containing this container,
// nextViewController is executed
- (void)nextViewController
{
    // If the view is still transitioning we must wait
    if (self.transitionInProgress) {
        return;
    }

    self.transitionInProgress = YES;

    // Set the next segue to occur based on what the current value is and what
    // the user has previously input
    self.currentSegueIdentifier = [self nextSegueForDirection:@"forward"];

    //increment curPage
    self.curPage++;
    [self.sessionBaseVC setProgressBarCurrentPage:self.curPage ofPages:self.numPages];


    // The following if-else block is for when we've navigated backwards and are coming
    // forward again. Rather than calling performSegueWithIdentifier, we already have
    // the next view controller on the stack, so just swap to it
    if (([self.currentSegueIdentifier isEqualToString:SegueIdentifierSecond]) && self.secondViewController) {
        [self swapFromViewController:self.currentChildController toViewController:self.secondViewController];
        return;
    }
    else if (([self.currentSegueIdentifier isEqualToString:SegueIdentifierThird]) && self.thirdViewController) {
        [self swapFromViewController:self.currentChildController toViewController:self.thirdViewController];
        return;
    }
    else if (([self.currentSegueIdentifier isEqualToString:SegueIdentifierFourth]) && self.fourthViewController) {
        [self swapFromViewController:self.currentChildController toViewController:self.fourthViewController];
        return;
    }
    else if (([self.currentSegueIdentifier isEqualToString:SegueIdentifierFifth]) && self.fifthViewController) {
        [self swapFromViewController:self.currentChildController toViewController:self.fifthViewController];
        return;
    }
    else if (([self.currentSegueIdentifier isEqualToString:SegueIdentifierSixth]) && self.sixthViewController) {
        [self swapFromViewController:self.currentChildController toViewController:self.sixthViewController];
        return;
    }

    [self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil];
    self.currentChildVCIndex++;
}

我使用容器视图的原因是我需要一个进度条以及前面提到的下一个和上一个问题按钮以保留在视图上,而不是每个问题都更改为“新”。

这是我的故事板截图的链接,紫色和绿色代表透明填充视图(紫色将包含进度条,绿色显示下一个和上一个按钮)。

http://i.imgur.com/R8rjnpj.png

所以提供所有信息,这是我的问题:

我正在使用自动布局约束来定位每个问题屏幕中的元素,但是当我使用自定义空segue时,容器的大小不会传播到这些子节点(就像使用push segue一样) 。因此,当我尝试调整这些视图的大小以适应iPhone 3.5“-5.5”以及所有iPad尺寸时,这会导致问题。 是否可以让子节点与接口构建器中的容器父节点保持相同的大小,以便我可以为每个大小类正确应用约束?

0 个答案:

没有答案