How do I get the frame of a subview in the ToViewController during animateTransition:?
Asked Answered
P

1

10

I an using the new custom transitions introduced with iOS 7 and I'm trying to move a view from my 'master' view controller to a view in my 'detail' view controller kind of like how Photos.app zooms in on a photo when you tap on it except this view in the detail vc only takes up half of the screen. To do this I'm trying to animate this view to it's final frame by getting the final frame from the detail or toViewController. However the frame I get is the starting frame from Interface Builder for the toViewController's view and not the view that appears after AutoLayer has taken a pass at it. How do I get the frame after it has been set by Auto Layout? I have tried [self.view layoutSubviews] right before the toViewController special view frame is returned to animateTransition: but that still gives the IB layout.

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    [[transitionContext containerView] addSubview:toViewController.view];
    toViewController.view.alpha = 0;

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        fromViewController.view.alpha = 0;
        toViewController.view.alpha = 1;
        fromViewController.specialView = [toViewController detailSpecialViewFrame];
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];

    }];

}
Perreault answered 26/8, 2014 at 13:15 Comment(4)
Did you get any solution to this?Interplay
@Alf Not a satisfactory one. It has been awhile since I've looked that this but I maybe recall calling the toViewController's view property in order for viewDidLoad to be called but I didn't like that solutions.Perreault
Thx. I got mine working using toView.layoutIfNeeded().Interplay
Anyone intrested I found the solution https://mcmap.net/q/1169145/-uiviewcontrolleranimatedtransitioning-with-safe-area-insets-on-iphone-xAircondition
A
1

Call layoutIfNeeded, not layoutSubviews, to force a layout pass. Then the frame should be updated.

Ambidexter answered 12/8, 2016 at 7:41 Comment(3)
Sadly this doesn't work. At least not in my scenario. The frames are still as IB, not the final layout. Layout isn't correct 'til viewDidAppear which isn't called 'til the transition is completed.Primulaceous
It's a chicken and egg problem. :/Osteotomy
check this out https://mcmap.net/q/1169145/-uiviewcontrolleranimatedtransitioning-with-safe-area-insets-on-iphone-xAircondition

© 2022 - 2024 — McMap. All rights reserved.