如何使用自动布局集装箱过渡? [英] How to use Auto Layout with container transitions?

查看:133
本文介绍了如何使用自动布局集装箱过渡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何使用自动布局与UIViewController的容器过渡方式:

   - (无效)transitionFromViewController:(UIViewController的*)fromViewController
                   toViewController:(UIViewController的*)toViewController
                           持续时间:(NSTimeInterval)持续时间
                            选项​​:(UIViewAnimationOptions)选项
                         动画:(无效(^)(无效))动画
                         完成:(无效(^)(BOOL完成))完成;

传统上,使用普林斯/ Struts中,你设置初始帧(只是调用此方法之前),并设置最后帧传递给该方法的动画块。

这方法确实添加视图视图层次结构和运行动画为你的工作。

的问题是,在同一地点你我们不能添加初始约束(方法调用之前),因为该视图还没有被添加到视图层级

任何想法如何,我可以自动布局使用此方法?

下面是一个例子(谢谢cocoanetics)使用普林斯/ Struts的(帧)这样做的
<一href=\"http://www.cocoanetics.com/2012/04/containing-viewcontrollers\">http://www.cocoanetics.com/2012/04/containing-viewcontrollers

   - (无效)transitionFromViewController:(UIViewController的*)fromViewController toViewController:(UIViewController的*)toViewController
{    // XXX我们不能在这里添加约束,因为该视图是尚未视图层次
    //设置动画
    toViewController.view.frame = _containerView.bounds;
    toViewController.view.autoresizingMask = _containerView.autoresizingMask;    //通知
    [fromViewController willMoveToParentViewController:无];
    [个体经营addChildViewController:toViewController];    // 过渡
    [个体经营transitionFromViewController:fromViewController
                      toViewController:toViewController
                              持续时间:1.0
                               选项​​:UIViewAnimationOptionTransitionCurlDown
                            动画:^ {
                            }
                            完成:^(BOOL完){
                                [toViewController didMoveToParentViewController:个体经营];
                                [fromViewController removeFromParentViewController]
                            }];
}


解决方案

开始觉得实用方法
transitionFromViewController:toViewController:持续时间:选择:动画:完成不能作出与自动布局干净利落工作

有关现在我已经取代了我使用这种方法通过调用每个低层次遏制直接的方法。这是一个比较code,但似乎给更多的控制。

它看起来是这样的:

   - (无效)performTransitionFromViewController:(UIViewController的*)fromVc toViewController:(UIViewController的*)toVc {    [fromVc willMoveToParentViewController:无];
    [个体经营addChildViewController:toVc];    UIView的* toView = toVc.view;
    UIView的* fromView = fromVc.view;    [self.containerView addSubview:toView];    // TODO:设置初始布局在这里限制    [self.containerView layoutIfNeeded]    [UIView的animateWithDuration:亮起0.25
                          延迟:0
                        选项​​:0
                     动画:^ {                         // TODO:设置最终布局在这里限制                         [self.containerView layoutIfNeeded]                     }完成:^(BOOL完){
                         [toVc didMoveToParentViewController:个体经营];
                         [fromView removeFromSuperview]
                         [fromVc removeFromParentViewController]
                     }];
}

How can you use Auto Layout with the UIViewController container transition method:

-(void)transitionFromViewController:(UIViewController *)fromViewController
                   toViewController:(UIViewController *)toViewController 
                           duration:(NSTimeInterval)duration
                            options:(UIViewAnimationOptions)options
                         animations:(void (^)(void))animations
                         completion:(void (^)(BOOL finished))completion;

Traditionally, using Springs/Struts, you set the initial frames (just before calling this method) and set up the final frames in the animation block you pass to the method.

That method does the work of adding the view to the view hierarchy and running the animations for you.

The problem is that you we can't add initial constraints in the same spot (before the method call) because the view has not yet been added to the view hierarchy.

Any ideas how I can use this method along with Auto Layout?

Below is an example (Thank you cocoanetics) of doing this using Springs/Struts (frames) http://www.cocoanetics.com/2012/04/containing-viewcontrollers

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{

    // XXX We can't add constraints here because the view is not yet in the view hierarchy
    // animation setup 
    toViewController.view.frame = _containerView.bounds;
    toViewController.view.autoresizingMask = _containerView.autoresizingMask;

    // notify
    [fromViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];

    // transition
    [self transitionFromViewController:fromViewController
                      toViewController:toViewController
                              duration:1.0
                               options:UIViewAnimationOptionTransitionCurlDown
                            animations:^{
                            }
                            completion:^(BOOL finished) {
                                [toViewController didMoveToParentViewController:self];
                                [fromViewController removeFromParentViewController];
                            }];
}

解决方案

Starting to think the utility method transitionFromViewController:toViewController:duration:options:animations:completion can not be made to work cleanly with Auto Layout.

For now I've replaced my use of this method with calls to each of the "lower level" containment methods directly. It is a bit more code but seems to give greater control.

It looks like this:

- (void) performTransitionFromViewController:(UIViewController*)fromVc toViewController:(UIViewController*)toVc {

    [fromVc willMoveToParentViewController:nil];
    [self addChildViewController:toVc];

    UIView *toView = toVc.view;
    UIView *fromView = fromVc.view;

    [self.containerView addSubview:toView];

    // TODO: set initial layout constraints here

    [self.containerView layoutIfNeeded];

    [UIView animateWithDuration:.25
                          delay:0
                        options:0
                     animations:^{

                         // TODO: set final layout constraints here

                         [self.containerView layoutIfNeeded];

                     } completion:^(BOOL finished) {
                         [toVc didMoveToParentViewController:self];
                         [fromView removeFromSuperview];
                         [fromVc removeFromParentViewController];
                     }];
}

这篇关于如何使用自动布局集装箱过渡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆