简体   繁体   English

故事板设计屏幕外子视图

[英]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. 我想使用Storyboard来设计滑块的内容,似乎设计脱机内容的一种简单方法是使用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 我试图这样做只是添加子视图(sliderView是我想要的子视图的名称),而不是整个视图,但这没做

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. 但是也许您想坚持使用Storyboard。 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. 而且,如果您有一些开发人员在从事此工作,那么保持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. 无需将其保存在单独的视图控制器中并在需要时对其进行屏幕动画处理时将其添加,只需将其添加到现有的视图控制器中并将其设置为隐藏,或在IB中将其alpha设置为0.0。 Then your animation can undo that and make it visible. 然后,您的动画可以撤消并使其可见。

you can use custom segue here, for instance: 您可以在此处使用自定义segue,例如:

@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 使您的UIStoryboardSegue子类
  • override - (void)perform method with your custom view appearance code 覆盖- (void)perform使用自定义视图外观代码- (void)perform方法
  • use segue usual way 通常使用segue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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