UIViewController+ChildViewController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // UIViewController+ChildViewController.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 14-2-24.
  6. // Copyright (c) 2014年 CommonLibrary. All rights reserved.
  7. //
  8. #import "UIViewController+ChildViewController.h"
  9. @implementation UIViewController (ChildViewController)
  10. - (void)addChild:(UIViewController *)vc
  11. {
  12. [self addChild:vc inRect:CGRectZero];
  13. }
  14. - (void)addChild:(UIViewController *)vc animation:(void (^)(void))inAnimation
  15. {
  16. [self addChild:vc inRect:CGRectZero animation:inAnimation];
  17. }
  18. - (void)addChild:(UIViewController *)vc inRect:(CGRect)rect
  19. {
  20. [self.view addSubview:vc.view];
  21. vc.view.frame = rect;
  22. [self addChildViewController:vc];
  23. }
  24. - (void)addChild:(UIViewController *)vc inRect:(CGRect)rect animation:(void (^)(void))inAnimation
  25. {
  26. [self addChild:vc inRect:rect];
  27. if (inAnimation)
  28. {
  29. [UIView animateWithDuration:0.3 animations:^{
  30. inAnimation();
  31. }];
  32. }
  33. }
  34. - (void)addChild:(UIViewController *)vc container:(UIView *)view
  35. {
  36. [view addSubview:vc.view];
  37. [self addChildViewController:vc];
  38. }
  39. - (void)addChild:(UIViewController *)vc container:(UIView *)view inRect:(CGRect)rect
  40. {
  41. [view addSubview:vc.view];
  42. vc.view.frame = rect;
  43. [self addChildViewController:vc];
  44. }
  45. - (void)addChild:(UIViewController *)child container:(UIView *)view inRect:(CGRect)rect animation:(void (^)(void))inAnimation
  46. {
  47. [self addChild:child container:view inRect:rect];
  48. if (inAnimation) {
  49. [UIView animateWithDuration:0.3 animations:^{
  50. inAnimation();
  51. }];
  52. }
  53. }
  54. - (void)removeChild:(UIViewController *)vc
  55. {
  56. [vc.view removeFromSuperview];
  57. [vc willMoveToParentViewController:nil];
  58. [vc removeFromParentViewController];
  59. }
  60. - (void)removeChild:(UIViewController *)vc animation:(void (^)(void))outAnimation
  61. {
  62. if (outAnimation)
  63. {
  64. [UIView animateWithDuration:0.3 animations:^{
  65. outAnimation();
  66. } completion:^(BOOL finished) {
  67. [self removeChild:vc];
  68. }];
  69. }
  70. else
  71. {
  72. [self removeChild:vc];
  73. }
  74. }
  75. - (void)removeChild:(UIViewController *)child inContainer:(UIView *)view
  76. {
  77. [child.view removeFromSuperview];
  78. [child willMoveToParentViewController:nil];
  79. [child removeFromParentViewController];
  80. }
  81. - (void)removeChild:(UIViewController *)child inContainer:(UIView *)view animation:(void (^)(void))outAnimation
  82. {
  83. if (outAnimation)
  84. {
  85. [UIView animateWithDuration:0.3 animations:^{
  86. outAnimation();
  87. } completion:^(BOOL finished) {
  88. [self removeChild:child inContainer:view];
  89. }];
  90. }
  91. else
  92. {
  93. [self removeChild:child inContainer:view];
  94. }
  95. }
  96. // 默认是在self.view中操作
  97. - (void)replace:(UIViewController *)oldVC withNew:(UIViewController *)newVC animations:(void (^)(void))animations;
  98. {
  99. [self replace:oldVC withNew:newVC container:self.view animations:animations];
  100. }
  101. - (void)replace:(UIViewController *)old withNew:(UIViewController *)newvc container:(UIView *)container animations:(void (^)(void))animations
  102. {
  103. if (newvc)
  104. {
  105. [self addChild:newvc container:container inRect:old.view.frame];
  106. if (old)
  107. {
  108. [self transitionFromViewController:old toViewController:newvc duration:0.0 options:0 animations:animations completion:^(BOOL finished)
  109. {
  110. [newvc didMoveToParentViewController:self];
  111. [[old view] removeFromSuperview];
  112. [old willMoveToParentViewController:nil];
  113. [old removeFromParentViewController];
  114. }];
  115. }
  116. else
  117. {
  118. [container addSubview:[newvc view]];
  119. [newvc didMoveToParentViewController:self];
  120. }
  121. }
  122. else
  123. {
  124. [[old view] removeFromSuperview];
  125. [old willMoveToParentViewController:nil];
  126. [old removeFromParentViewController];
  127. }
  128. }
  129. @end