RCTWrapperViewControllerHostingView.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTWrapperViewControllerHostingView.h"
  8. #import <React/UIView+React.h>
  9. #pragma mark - UIViewController+Children
  10. @interface UIViewController (Children)
  11. @property (nonatomic, readonly) BOOL isAttached;
  12. - (void)attachChildViewController:(UIViewController *)childViewController toContainerView:(UIView *)containerView;
  13. - (void)detachChildViewController:(UIViewController *)childViewController;
  14. @end
  15. @implementation UIViewController (Children)
  16. - (BOOL)isAttached
  17. {
  18. return self.parentViewController != nil;
  19. }
  20. - (void)attachChildViewController:(UIViewController *)childViewController toContainerView:(UIView *)containerView
  21. {
  22. [self addChildViewController:childViewController];
  23. // `[childViewController willMoveToParentViewController:self]` is calling automatically
  24. [containerView addSubview:childViewController.view];
  25. childViewController.view.frame = containerView.bounds;
  26. childViewController.view.translatesAutoresizingMaskIntoConstraints = YES;
  27. childViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  28. [childViewController didMoveToParentViewController:self];
  29. [childViewController beginAppearanceTransition:true animated:false];
  30. [childViewController endAppearanceTransition];
  31. }
  32. - (void)detachChildViewController:(UIViewController *)childViewController
  33. {
  34. [childViewController beginAppearanceTransition:false animated: false];
  35. [childViewController endAppearanceTransition];
  36. [childViewController willMoveToParentViewController:nil];
  37. [childViewController.view removeFromSuperview];
  38. [childViewController removeFromParentViewController];
  39. // `[childViewController didMoveToParentViewController:nil]` is calling automatically
  40. }
  41. @end
  42. @implementation RCTWrapperViewControllerHostingView {
  43. UIViewController *_Nullable _contentViewController;
  44. }
  45. #pragma mark - `contentViewController`
  46. - (nullable UIViewController *)contentViewController
  47. {
  48. return _contentViewController;
  49. }
  50. - (void)setContentViewController:(UIViewController *)contentViewController
  51. {
  52. if (_contentViewController) {
  53. [self detachContentViewControllerIfNeeded];
  54. }
  55. _contentViewController = contentViewController;
  56. if (_contentViewController) {
  57. [self attachContentViewControllerIfNeeded];
  58. }
  59. }
  60. #pragma mark - Attaching and Detaching
  61. - (void)attachContentViewControllerIfNeeded
  62. {
  63. if (self.contentViewController.isAttached) {
  64. return;
  65. }
  66. [self.reactViewController attachChildViewController:self.contentViewController toContainerView:self];
  67. }
  68. - (void)detachContentViewControllerIfNeeded
  69. {
  70. if (!self.contentViewController.isAttached) {
  71. return;
  72. }
  73. [self.reactViewController detachChildViewController:self.contentViewController];
  74. }
  75. #pragma mark - Life cycle
  76. - (void)willMoveToWindow:(UIWindow *)newWindow
  77. {
  78. if (newWindow == nil) {
  79. [self detachContentViewControllerIfNeeded];
  80. }
  81. }
  82. - (void)didMoveToWindow
  83. {
  84. [super didMoveToWindow];
  85. [self attachContentViewControllerIfNeeded];
  86. }
  87. #pragma mark - Layout
  88. - (void)setNeedsLayout
  89. {
  90. [super setNeedsLayout];
  91. [self.superview setNeedsLayout];
  92. }
  93. - (CGSize)intrinsicContentSize
  94. {
  95. return self.contentViewController.view.intrinsicContentSize;
  96. }
  97. - (CGSize)sizeThatFits:(CGSize)size
  98. {
  99. return [self.contentViewController.view sizeThatFits:size];
  100. }
  101. @end