Guest User

Untitled

a guest
Mar 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. (lldb) po [[UIWindow keyWindow] _autolayoutTrace]
  2.  
  3. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  4. animationControllerForOperation:(UINavigationControllerOperation)operation
  5. fromViewController:(UIViewController*)fromVC
  6. toViewController:(UIViewController*)toVC
  7. {
  8. if (operation == UINavigationControllerOperationPush)
  9. return [[PushAnimator alloc] init];
  10.  
  11. return nil;
  12. }
  13.  
  14. @implementation PushAnimator
  15.  
  16. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
  17. {
  18. return 0.5;
  19. }
  20.  
  21. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  22. {
  23. UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  24. UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  25.  
  26. [[transitionContext containerView] addSubview:toViewController.view];
  27. CGFloat width = fromViewController.view.frame.size.width;
  28.  
  29. toViewController.view.transform = CGAffineTransformMakeTranslation(width, 0);
  30.  
  31. [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
  32. fromViewController.view.transform = CGAffineTransformMakeTranslation(-width / 2.0, 0);
  33. toViewController.view.transform = CGAffineTransformIdentity;
  34. } completion:^(BOOL finished) {
  35. fromViewController.view.transform = CGAffineTransformIdentity;
  36. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  37. }];
  38. }
  39.  
  40. @end
  41.  
  42. func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
  43. let fromView = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!.view
  44. let destinationVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
  45. destinationVC.view.frame = transitionContext.finalFrameForViewController(destinationVC)
  46. let container = transitionContext.containerView()
  47. container.addSubview(destinationVC.view)
  48.  
  49. // Custom transitions break topLayoutGuide in iOS 7, fix its constraint
  50. if let navController = destinationVC.navigationController {
  51. for constraint in destinationVC.view.constraints() as [NSLayoutConstraint] {
  52. if constraint.firstItem === destinationVC.topLayoutGuide
  53. && constraint.firstAttribute == .Height
  54. && constraint.secondItem == nil
  55. && constraint.constant == 0 {
  56. constraint.constant = navController.navigationBar.frame.height
  57. }
  58. }
  59. }
  60.  
  61. // Perform your transition animation here ...
  62. }
  63.  
  64. toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
  65.  
  66. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {
  67.  
  68. // Add the toView to the container
  69. UIView* containerView = [transitionContext containerView];
  70. [containerView addSubview:toView];
  71. [containerView sendSubviewToBack:toView];
  72.  
  73.  
  74. // animate
  75. toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
  76. NSTimeInterval duration = [self transitionDuration:transitionContext];
  77. [UIView animateWithDuration:duration animations:^{
  78. fromView.alpha = 0.0;
  79. } completion:^(BOOL finished) {
  80. if ([transitionContext transitionWasCancelled]) {
  81. fromView.alpha = 1.0;
  82. } else {
  83. // reset from- view to its original state
  84. [fromView removeFromSuperview];
  85. fromView.alpha = 1.0;
  86. }
  87. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  88. }];
  89.  
  90. }
  91.  
  92. self.edgesForExtendedLayout = UIRectEdgeNone;
  93.  
  94. CGFloat navigationBarHeight = toViewController.navigationController.navigationBar.frame.size.height;
  95.  
  96. for (NSLayoutConstraint *constraint in toViewController.view.constraints) {
  97. if (constraint.firstItem == toViewController.topLayoutGuide
  98. && constraint.firstAttribute == NSLayoutAttributeHeight
  99. && constraint.secondItem == nil
  100. && constraint.constant < navigationBarHeight) {
  101. constraint.constant += navigationBarHeight;
  102. }
  103. }
  104.  
  105. self.extendedLayoutIncludesOpaqueBars = YES;
  106.  
  107. @implementation UIViewController (hp_layoutGuideFix)
  108.  
  109. - (BOOL)hp_usesTopLayoutGuideInConstraints
  110. {
  111. return NO;
  112. }
  113.  
  114. - (id<UILayoutSupport>)hp_topLayoutGuide
  115. {
  116. id<UILayoutSupport> object = objc_getAssociatedObject(self, @selector(hp_topLayoutGuide));
  117. return object ? : self.topLayoutGuide;
  118. }
  119.  
  120. - (void)setHp_topLayoutGuide:(id<UILayoutSupport>)hp_topLayoutGuide
  121. {
  122. HPLayoutSupport *object = objc_getAssociatedObject(self, @selector(hp_topLayoutGuide));
  123. if (object != nil && self.hp_usesTopLayoutGuideInConstraints)
  124. {
  125. [object removeFromSuperview];
  126. }
  127. HPLayoutSupport *layoutGuide = [[HPLayoutSupport alloc] initWithLength:hp_topLayoutGuide.length];
  128. if (self.hp_usesTopLayoutGuideInConstraints)
  129. {
  130. [self.view addSubview:layoutGuide];
  131. }
  132. objc_setAssociatedObject(self, @selector(hp_topLayoutGuide), layoutGuide, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  133. }
  134.  
  135. @end
  136.  
  137. @implementation HPLayoutSupport {
  138. CGFloat _length;
  139. }
  140.  
  141. - (id)initWithLength:(CGFloat)length
  142. {
  143. self = [super init];
  144. if (self)
  145. {
  146. self.translatesAutoresizingMaskIntoConstraints = NO;
  147. self.userInteractionEnabled = NO;
  148. _length = length;
  149. }
  150. return self;
  151. }
  152.  
  153. - (CGSize)intrinsicContentSize
  154. {
  155. return CGSizeMake(1, _length);
  156. }
  157.  
  158. - (CGFloat)length
  159. {
  160. return _length;
  161. }
  162.  
  163. @end
  164.  
  165. - (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  166. animationControllerForOperation:(UINavigationControllerOperation)operation
  167. fromViewController:(UIViewController *)fromVC
  168. toViewController:(UIViewController *)toVC
  169. {
  170. toVC.hp_topLayoutGuide = fromVC.hp_topLayoutGuide;
  171. id <UIViewControllerAnimatedTransitioning> animator;
  172. // Initialise animator
  173. return animator;
  174. }
  175.  
  176. - (void)updateViewConstraints
  177. {
  178. [super updateViewConstraints];
  179. id<UILayoutSupport> topLayoutGuide = self.hp_topLayoutGuide;
  180. // Example constraint
  181. NSDictionary *views = NSDictionaryOfVariableBindings(_imageView, _dateLabel, topLayoutGuide);
  182. NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topLayoutGuide][_imageView(240)]-8-[_dateLabel]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views];
  183. [self.view addConstraints:constraints];
  184. }
  185.  
  186. - (BOOL)hp_usesTopLayoutGuideInConstraints
  187. {
  188. return YES;
  189. }
  190.  
  191. self.edgesforextendedlayout=UIRectEdgeNone
  192.  
  193. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
  194. {
  195. UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  196. UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  197. UIView *container = [transitionContext containerView];
  198.  
  199. CGAffineTransform destinationTransform;
  200. UIViewController *targetVC;
  201. CGFloat adjustmentForIOS7AutoLayoutBug = 0.0f;
  202.  
  203. // We're doing a view controller POP
  204. if(self.isViewControllerPop)
  205. {
  206. targetVC = fromViewController;
  207.  
  208. [container insertSubview:toViewController.view belowSubview:fromViewController.view];
  209.  
  210. // Only need this auto layout hack in iOS7; it's fixed in iOS8
  211. if(_device_is_running_iOS7_)
  212. {
  213. adjustmentForIOS7AutoLayoutBug = toViewController.navigationController.navigationBar.frame.size.height;
  214. [toViewController.view setFrameOriginY:adjustmentForIOS7AutoLayoutBug];
  215. }
  216.  
  217. destinationTransform = CGAffineTransformMakeTranslation(fromViewController.view.bounds.size.width,adjustmentForIOS7AutoLayoutBug);
  218. }
  219. // We're doing a view controller PUSH
  220. else
  221. {
  222. targetVC = toViewController;
  223.  
  224. [container addSubview:toViewController.view];
  225.  
  226. // Only need this auto layout hack in iOS7; it's fixed in iOS8
  227. if(_device_is_running_iOS7_)
  228. {
  229. adjustmentForIOS7AutoLayoutBug = toViewController.navigationController.navigationBar.frame.size.height;
  230. }
  231.  
  232. toViewController.view.transform = CGAffineTransformMakeTranslation(toViewController.view.bounds.size.width,adjustmentForIOS7AutoLayoutBug);
  233. destinationTransform = CGAffineTransformMakeTranslation(0.0f,adjustmentForIOS7AutoLayoutBug);
  234. }
  235.  
  236. [UIView animateWithDuration:_animation_duration_
  237. delay:_animation_delay_if_you_need_one_
  238. options:([transitionContext isInteractive] ? UIViewAnimationOptionCurveLinear : UIViewAnimationOptionCurveEaseOut)
  239. animations:^(void)
  240. {
  241. targetVC.view.transform = destinationTransform;
  242. }
  243. completion:^(BOOL finished)
  244. {
  245. [transitionContext completeTransition:([transitionContext transitionWasCancelled] ? NO : YES)];
  246. }];
  247. }
  248.  
  249. class MyTransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
  250.  
  251. private var presenting = true
  252. private var container:UIView?
  253. private var safeInsets:UIEdgeInsets?
  254.  
  255. ...
  256.  
  257. let toView = viewControllers.to.view
  258. let fromView = viewControllers.from.view
  259.  
  260. if #available(iOS 11.0, *) {
  261. safeInsets = toView.safeAreaInsets
  262. }
  263.  
  264. if #available(iOS 11.0, *) {
  265. if safeInsets != nil {
  266. viewControllers.to.additionalSafeAreaInsets = safeInsets!
  267. }
  268. }
  269.  
  270. ...then in the animation completion block
  271.  
  272. if #available(iOS 11.0, *) {
  273. if self.safeInsets != nil {
  274. viewControllers.to.additionalSafeAreaInsets = .zero
  275. }
  276. }
  277.  
  278. transitionContext.completeTransition(true)
  279.  
  280. float y = toVC.navigationController.navigationBar.frame.origin.y + toVC.navigationController.navigationBar.frame.size.height;
  281. 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