Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (lldb) po [[UIWindow keyWindow] _autolayoutTrace]
- - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
- animationControllerForOperation:(UINavigationControllerOperation)operation
- fromViewController:(UIViewController*)fromVC
- toViewController:(UIViewController*)toVC
- {
- if (operation == UINavigationControllerOperationPush)
- return [[PushAnimator alloc] init];
- return nil;
- }
- @implementation PushAnimator
- - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
- {
- return 0.5;
- }
- - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
- {
- UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
- UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
- [[transitionContext containerView] addSubview:toViewController.view];
- CGFloat width = fromViewController.view.frame.size.width;
- toViewController.view.transform = CGAffineTransformMakeTranslation(width, 0);
- [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
- fromViewController.view.transform = CGAffineTransformMakeTranslation(-width / 2.0, 0);
- toViewController.view.transform = CGAffineTransformIdentity;
- } completion:^(BOOL finished) {
- fromViewController.view.transform = CGAffineTransformIdentity;
- [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
- }];
- }
- @end
- func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
- let fromView = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!.view
- let destinationVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
- destinationVC.view.frame = transitionContext.finalFrameForViewController(destinationVC)
- let container = transitionContext.containerView()
- container.addSubview(destinationVC.view)
- // Custom transitions break topLayoutGuide in iOS 7, fix its constraint
- if let navController = destinationVC.navigationController {
- for constraint in destinationVC.view.constraints() as [NSLayoutConstraint] {
- if constraint.firstItem === destinationVC.topLayoutGuide
- && constraint.firstAttribute == .Height
- && constraint.secondItem == nil
- && constraint.constant == 0 {
- constraint.constant = navController.navigationBar.frame.height
- }
- }
- }
- // Perform your transition animation here ...
- }
- toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
- - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {
- // Add the toView to the container
- UIView* containerView = [transitionContext containerView];
- [containerView addSubview:toView];
- [containerView sendSubviewToBack:toView];
- // animate
- toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
- NSTimeInterval duration = [self transitionDuration:transitionContext];
- [UIView animateWithDuration:duration animations:^{
- fromView.alpha = 0.0;
- } completion:^(BOOL finished) {
- if ([transitionContext transitionWasCancelled]) {
- fromView.alpha = 1.0;
- } else {
- // reset from- view to its original state
- [fromView removeFromSuperview];
- fromView.alpha = 1.0;
- }
- [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
- }];
- }
- self.edgesForExtendedLayout = UIRectEdgeNone;
- CGFloat navigationBarHeight = toViewController.navigationController.navigationBar.frame.size.height;
- for (NSLayoutConstraint *constraint in toViewController.view.constraints) {
- if (constraint.firstItem == toViewController.topLayoutGuide
- && constraint.firstAttribute == NSLayoutAttributeHeight
- && constraint.secondItem == nil
- && constraint.constant < navigationBarHeight) {
- constraint.constant += navigationBarHeight;
- }
- }
- self.extendedLayoutIncludesOpaqueBars = YES;
- @implementation UIViewController (hp_layoutGuideFix)
- - (BOOL)hp_usesTopLayoutGuideInConstraints
- {
- return NO;
- }
- - (id<UILayoutSupport>)hp_topLayoutGuide
- {
- id<UILayoutSupport> object = objc_getAssociatedObject(self, @selector(hp_topLayoutGuide));
- return object ? : self.topLayoutGuide;
- }
- - (void)setHp_topLayoutGuide:(id<UILayoutSupport>)hp_topLayoutGuide
- {
- HPLayoutSupport *object = objc_getAssociatedObject(self, @selector(hp_topLayoutGuide));
- if (object != nil && self.hp_usesTopLayoutGuideInConstraints)
- {
- [object removeFromSuperview];
- }
- HPLayoutSupport *layoutGuide = [[HPLayoutSupport alloc] initWithLength:hp_topLayoutGuide.length];
- if (self.hp_usesTopLayoutGuideInConstraints)
- {
- [self.view addSubview:layoutGuide];
- }
- objc_setAssociatedObject(self, @selector(hp_topLayoutGuide), layoutGuide, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- @end
- @implementation HPLayoutSupport {
- CGFloat _length;
- }
- - (id)initWithLength:(CGFloat)length
- {
- self = [super init];
- if (self)
- {
- self.translatesAutoresizingMaskIntoConstraints = NO;
- self.userInteractionEnabled = NO;
- _length = length;
- }
- return self;
- }
- - (CGSize)intrinsicContentSize
- {
- return CGSizeMake(1, _length);
- }
- - (CGFloat)length
- {
- return _length;
- }
- @end
- - (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
- animationControllerForOperation:(UINavigationControllerOperation)operation
- fromViewController:(UIViewController *)fromVC
- toViewController:(UIViewController *)toVC
- {
- toVC.hp_topLayoutGuide = fromVC.hp_topLayoutGuide;
- id <UIViewControllerAnimatedTransitioning> animator;
- // Initialise animator
- return animator;
- }
- - (void)updateViewConstraints
- {
- [super updateViewConstraints];
- id<UILayoutSupport> topLayoutGuide = self.hp_topLayoutGuide;
- // Example constraint
- NSDictionary *views = NSDictionaryOfVariableBindings(_imageView, _dateLabel, topLayoutGuide);
- NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topLayoutGuide][_imageView(240)]-8-[_dateLabel]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views];
- [self.view addConstraints:constraints];
- }
- - (BOOL)hp_usesTopLayoutGuideInConstraints
- {
- return YES;
- }
- self.edgesforextendedlayout=UIRectEdgeNone
- - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
- {
- UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
- UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
- UIView *container = [transitionContext containerView];
- CGAffineTransform destinationTransform;
- UIViewController *targetVC;
- CGFloat adjustmentForIOS7AutoLayoutBug = 0.0f;
- // We're doing a view controller POP
- if(self.isViewControllerPop)
- {
- targetVC = fromViewController;
- [container insertSubview:toViewController.view belowSubview:fromViewController.view];
- // Only need this auto layout hack in iOS7; it's fixed in iOS8
- if(_device_is_running_iOS7_)
- {
- adjustmentForIOS7AutoLayoutBug = toViewController.navigationController.navigationBar.frame.size.height;
- [toViewController.view setFrameOriginY:adjustmentForIOS7AutoLayoutBug];
- }
- destinationTransform = CGAffineTransformMakeTranslation(fromViewController.view.bounds.size.width,adjustmentForIOS7AutoLayoutBug);
- }
- // We're doing a view controller PUSH
- else
- {
- targetVC = toViewController;
- [container addSubview:toViewController.view];
- // Only need this auto layout hack in iOS7; it's fixed in iOS8
- if(_device_is_running_iOS7_)
- {
- adjustmentForIOS7AutoLayoutBug = toViewController.navigationController.navigationBar.frame.size.height;
- }
- toViewController.view.transform = CGAffineTransformMakeTranslation(toViewController.view.bounds.size.width,adjustmentForIOS7AutoLayoutBug);
- destinationTransform = CGAffineTransformMakeTranslation(0.0f,adjustmentForIOS7AutoLayoutBug);
- }
- [UIView animateWithDuration:_animation_duration_
- delay:_animation_delay_if_you_need_one_
- options:([transitionContext isInteractive] ? UIViewAnimationOptionCurveLinear : UIViewAnimationOptionCurveEaseOut)
- animations:^(void)
- {
- targetVC.view.transform = destinationTransform;
- }
- completion:^(BOOL finished)
- {
- [transitionContext completeTransition:([transitionContext transitionWasCancelled] ? NO : YES)];
- }];
- }
- class MyTransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
- private var presenting = true
- private var container:UIView?
- private var safeInsets:UIEdgeInsets?
- ...
- let toView = viewControllers.to.view
- let fromView = viewControllers.from.view
- if #available(iOS 11.0, *) {
- safeInsets = toView.safeAreaInsets
- }
- if #available(iOS 11.0, *) {
- if safeInsets != nil {
- viewControllers.to.additionalSafeAreaInsets = safeInsets!
- }
- }
- ...then in the animation completion block
- if #available(iOS 11.0, *) {
- if self.safeInsets != nil {
- viewControllers.to.additionalSafeAreaInsets = .zero
- }
- }
- transitionContext.completeTransition(true)
- float y = toVC.navigationController.navigationBar.frame.origin.y + toVC.navigationController.navigationBar.frame.size.height;
- toVC.view.frame = CGRectMake(0, y, toVC.view.frame.size.width, toVC.view.frame.size.height - y);
Add Comment
Please, Sign In to add comment