NavigationViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // NavigationViewController.m
  3. //
  4. // Created by Alexi on 13-7-3.
  5. // Copyright (c) 2013年 . All rights reserved.
  6. //
  7. #import "NavigationViewController.h"
  8. #import "UINavigationController+Transition.h"
  9. #import "UIViewController+Layout.h"
  10. @interface NavigationViewController ()
  11. // 用户自定义的返回事件监听
  12. // pop的时候执行,其他时候置空不处理
  13. // 暂时无法使用,需要调研
  14. //@property (nonatomic, copy) FWVoidBlock backAction;
  15. @end
  16. @implementation NavigationViewController
  17. - (instancetype)initWithRootViewController:(UIViewController *)rootViewController
  18. {
  19. if (self = [super initWithRootViewController:rootViewController])
  20. {
  21. // self.navigationBar.tintColor = kNavBarThemeColor;
  22. // self.navigationBar.barTintColor = kNavBarThemeColor;
  23. self.edgesForExtendedLayout = UIRectEdgeNone;
  24. self.extendedLayoutIncludesOpaqueBars = NO;
  25. }
  26. return self;
  27. }
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. // [self setNavigationBarAppearance];
  32. }
  33. - (void)setNavigationBarAppearance
  34. {
  35. [self.navigationBar setTintColor:kWhiteColor];
  36. [self.navigationBar setBarTintColor:kWhiteColor];
  37. [self.navigationBar setTitleTextAttributes:@{
  38. NSForegroundColorAttributeName:kWhiteColor,
  39. NSFontAttributeName:[UIFont boldSystemFontOfSize:16]
  40. }];
  41. }
  42. - (BOOL)shouldAutorotate
  43. {
  44. BOOL rorate = [self.viewControllers.lastObject shouldAutorotate];
  45. return rorate;
  46. }
  47. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  48. {
  49. return [self.viewControllers.lastObject supportedInterfaceOrientations];
  50. }
  51. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  52. {
  53. return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
  54. }
  55. //ios5.0 横竖屏
  56. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  57. {
  58. return [self shouldAutorotate];
  59. }
  60. - (void)viewWillLayoutSubviews
  61. {
  62. if (![self asChild])
  63. {
  64. [super viewWillLayoutSubviews];
  65. }
  66. else
  67. {
  68. if (CGSizeEqualToSize(self.childSize, CGSizeZero)) {
  69. [super viewWillLayoutSubviews];
  70. }
  71. else
  72. {
  73. CGSize size = self.childSize;
  74. self.view.bounds = CGRectMake(0, 0, size.width, size.height);
  75. }
  76. }
  77. }
  78. - (void)pushAnimation:(UIViewController *)viewController
  79. {
  80. [self.view.layer addAnimation:[self pushAnimation] forKey:kCATransition];
  81. }
  82. - (void)popAnimation:(UIViewController *)viewController
  83. {
  84. [self.view.layer addAnimation:[self popAnimation] forKey:kCATransition];
  85. }
  86. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  87. {
  88. [self pushViewController:viewController withBackTitle:nil action:nil animated:animated];
  89. }
  90. - (void)pushViewController:(UIViewController *)viewController withBackTitle:(NSString *)backTitle animated:(BOOL)animated
  91. {
  92. [self pushViewController:viewController withBackTitle:backTitle action:nil animated:animated];
  93. }
  94. - (void)pushViewController:(UIViewController *)viewController withBackTitle:(NSString *)backTitle action:(FWVoidBlock)backAction animated:(BOOL)animated
  95. {
  96. if (backTitle.length != 0 )
  97. {
  98. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
  99. backItem.title = backTitle;
  100. self.topViewController.navigationItem.backBarButtonItem = backItem;
  101. }
  102. if (animated)
  103. {
  104. [super pushViewController:viewController animated:YES];
  105. return;
  106. }
  107. UIViewController *pushV = (UIViewController *) self.topViewController;
  108. if ([pushV respondsToSelector:@selector(pushAnimation:)])
  109. {
  110. [pushV performSelector:@selector(pushAnimation:) withObject:viewController];
  111. [super pushViewController:viewController animated:NO];
  112. }
  113. else
  114. {
  115. [super pushViewController:viewController animated:NO];
  116. [self pushAnimation:pushV];
  117. }
  118. }
  119. - (void)popAnimationOver
  120. {
  121. [super popViewControllerAnimated:NO];
  122. }
  123. - (UIViewController *)popViewControllerAnimated:(BOOL)animated
  124. {
  125. // if (self.backAction)
  126. // {
  127. // self.backAction();
  128. // return nil;
  129. // }
  130. if (animated)
  131. {
  132. return [super popViewControllerAnimated:YES];
  133. }
  134. UIViewController *popV = (UIViewController *) self.topViewController;
  135. if ([popV respondsToSelector:@selector(popAnimation:)])
  136. {
  137. [popV performSelector:@selector(popAnimation:) withObject:popV];
  138. return [super popViewControllerAnimated:NO];
  139. }
  140. else
  141. {
  142. [self popAnimation:popV];
  143. return [super popViewControllerAnimated:NO];
  144. }
  145. }
  146. - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
  147. {
  148. // self.backAction = nil;
  149. if (animated)
  150. {
  151. return [super popToRootViewControllerAnimated:animated];
  152. }
  153. [self.view.layer addAnimation:[self popAnimation] forKey:kCATransition];
  154. return [super popToRootViewControllerAnimated:NO];
  155. }
  156. - (void)pushViewControllerFromBottom:(UIViewController *)viewController animated:(BOOL)animated
  157. {
  158. [super pushViewController:viewController animated:NO];
  159. CATransition* transition = [CATransition animation];
  160. transition.duration = 0.3;
  161. transition.type = kCATransitionMoveIn;
  162. transition.subtype = kCATransitionFromTop;
  163. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  164. transition.autoreverses = NO;
  165. [self.view.layer addAnimation:transition forKey:kCATransition];
  166. }
  167. - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
  168. {
  169. // self.backAction = nil;
  170. if (!animated)
  171. {
  172. return [super popToViewController:viewController animated:animated];
  173. }
  174. UIViewController *popV = (UIViewController *) self.topViewController;
  175. if ([popV respondsToSelector:@selector(popAnimation:)])
  176. {
  177. [popV performSelector:@selector(popAnimation:) withObject:popV];
  178. return [super popToViewController:viewController animated:NO];
  179. }
  180. else
  181. {
  182. [self popAnimation:popV];
  183. return [super popToViewController:viewController animated:NO];
  184. }
  185. }
  186. - (void)layoutSubviewsFrame
  187. {
  188. [super layoutSubviewsFrame];
  189. [self.topViewController layoutSubviewsFrame];
  190. }
  191. - (UIStatusBarStyle)preferredStatusBarStyle
  192. {
  193. return [self.topViewController preferredStatusBarStyle];
  194. }
  195. @end