简体   繁体   中英

Storyboard to design off screen subview

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**);

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM