2

I want to use Storyboards to design content for a slider, and it seems like an easy way to design offscreen content is to use a childViewController. So I've done this

myViewController = [[UIStoryboard storyboardWithName:@"ipad" bundle:NULL] instantiateViewControllerWithIdentifier:@"keyPadOffScreen"];

[self addChildViewController:myViewController];
[myViewController didMoveToParentViewController:self];
newView = myViewController.view;
[self.view addSubview:newView];

And that adds the entire view controller over top of my root view. The problem is, I only want one of the subviews to show up, not the whole view. I can handle the animation, as long as I know how to add the root view. I tried this to just add the subview (sliderView is the name of the subview I want) instead of the whole view, but that did nothing

newView = myViewController.sliderView;
[self.view addSubview:newView];

Should I be using a different strategy?

EDIT: this DOES work, but it seems silly - setting the views size to just be the size of the subview.

  newView.frame = CGRectMake(newView.frame.origin.x, newView.frame.origin.y, newView.frame.size.width, **myViewController.sliderView.frame.size.height**);
Tommy Nicholas
  • 1,133
  • 5
  • 20
  • 31

2 Answers2

1

It does seem a bit overkill for just a view. Once you start doing a lot of custom view/animation/transition stuff it's often easier to implement in code, or at least it is for me since I've been doing it that way for a long time.

But maybe you want to stick with Storyboards. I respect that. And if you have a few developers working on this then it's important to keep some uniformity to how you set up your UI.

Instead of keeping it in a separate view controller and adding it when you need it to animate on-screen, simply add it to your existing view controller and either set it to hidden, or set it's alpha to 0.0 in IB. Then your animation can undo that and make it visible.

Evan Davis
  • 462
  • 1
  • 4
  • 9
  • I did the whole thing in code, but it didn't make sense because we're using Storyboards generally anyway (and we want to continue so that non iPhone developers can make tweaks to layouts) and it didn't make sense to be doing both. So the downside of doing it with a View Controller is it's going to slow down things, or just that it's overly complex? You're suggesting just deal with it all on the same View Controller and that can make a Storyboard messy, BUUUT it seems to make a lot of sense. I'm gonna try that. – Tommy Nicholas Dec 20 '13 at 00:51
0

you can use custom segue here, for instance:

@implementation FRPresentEnteringPopupSegue

- (void)perform
{
    FirstVC *toVC = self.destinationViewController;
    SecondNavigationController *fromVC = self.sourceViewController;
    toVC.view.frame = CGRectMake(0.0, 0.0, 300.0, 135.0);
    toVC.view.center = CGPointMake(fromVC.view.bounds.size.width/2, fromVC.view.bounds.size.height + toVC.view.bounds.size.height/2);
    [fromVC.view addSubview:toVC.view];
    [toVC viewWillAppear:YES];
    [UIView animateWithDuration:0.5
                          delay:0.0
         usingSpringWithDamping:0.7
          initialSpringVelocity:0.5
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         toVC.view.center = CGPointMake(fromVC.view.bounds.size.width/2, fromVC.view.bounds.size.height/2);
                     }completion:^(BOOL finished) {
                         [toVC viewDidAppear:YES];
                     }];
}

@end
  • make your UIStoryboardSegue subclass
  • override - (void)perform method with your custom view appearance code
  • use segue usual way
iiFreeman
  • 5,165
  • 2
  • 29
  • 42
  • Whoa not what I was looking for at all, but a pretty darn interesting alternate solution! – Tommy Nicholas Dec 20 '13 at 01:28
  • Yeah, works fine for me, but for dismiss you also need to create custom segue. And looks like segue always instantiating new destination view controller, you'll need to retain presented VC somehow in sourceViewController and access exactly to same instance to dismiss. It's not a big deal to figure it out, good luck! – iiFreeman Dec 20 '13 at 01:38