YPTabBarController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // YPTabBarController.m
  3. // YPTabBarController
  4. //
  5. // Created by 喻平 on 15/8/11.
  6. // Copyright (c) 2015年 YPTabBarController. All rights reserved.
  7. //
  8. #import "YPTabBarController.h"
  9. #import <objc/runtime.h>
  10. #define TAB_BAR_HEIGHT 50
  11. @interface YPTabBarController () {
  12. BOOL _didViewAppeared;
  13. }
  14. @property (nonatomic, strong) UIScrollView *scrollView;
  15. @property (nonatomic, assign) BOOL contentScrollEnabled;
  16. @property (nonatomic, assign) BOOL contentSwitchAnimated;
  17. @property (nonatomic, weak) UIView *line_View;
  18. @end
  19. @implementation YPTabBarController
  20. - (instancetype)init {
  21. self = [super init];
  22. if (self) {
  23. [self awakeFromNib];
  24. }
  25. return self;
  26. }
  27. - (instancetype)initWithCoder:(NSCoder *)coder {
  28. self = [super initWithCoder:coder];
  29. if (self) {
  30. }
  31. return self;
  32. }
  33. - (void)viewWillAppear:(BOOL)animated{
  34. [super viewWillAppear:animated];
  35. [super viewWillAppear:animated];
  36. if (!_didViewAppeared) {
  37. self.tabBar.selectedItemIndex = 0;
  38. _didViewAppeared = YES;
  39. }
  40. [g_notify addObserver:self selector:@selector(tabarFrame:) name:@"tabbarFrame" object:nil];
  41. }
  42. - (void)tabarFrame:(NSNotification *)note{
  43. NSDictionary *noteDict=note.userInfo;
  44. if ([noteDict[@"title"] intValue]==1) {
  45. CGSize screenSize = [UIScreen mainScreen].bounds.size;
  46. CGFloat navigationAndStatusBarHeight = 0;
  47. if (self.navigationController) {
  48. navigationAndStatusBarHeight = self.navigationController.navigationBar.frame.size.height + 20;
  49. }
  50. self.tabBar.frame = CGRectMake(0,
  51. screenSize.height - TAB_BAR_HEIGHT - 0-(JX_SCREEN_HEIGHT>=812?90:65),
  52. screenSize.width,
  53. (JX_SCREEN_HEIGHT>=812?60:50));
  54. UIView *line_View =[[UIView alloc]initWithFrame:CGRectMake(0, screenSize.height - 50 - 0-(JX_SCREEN_HEIGHT>=812?90:65), JX_SCREEN_WIDTH,1)];
  55. line_View.backgroundColor=kRGBColor(246, 246, 246);
  56. [self.view addSubview:line_View];
  57. self.line_View=line_View;
  58. [self.line_View setHidden:NO];
  59. self.contentViewFrame = CGRectMake(0, 0, screenSize.width,
  60. screenSize.height - TAB_BAR_HEIGHT - navigationAndStatusBarHeight-(JX_SCREEN_HEIGHT>=812?90:65));
  61. }else{
  62. CGSize screenSize = [UIScreen mainScreen].bounds.size;
  63. CGFloat navigationAndStatusBarHeight = 0;
  64. if (self.navigationController) {
  65. navigationAndStatusBarHeight = self.navigationController.navigationBar.frame.size.height + 20;
  66. }
  67. self.tabBar.frame = CGRectMake(0, screenSize.height, screenSize.width,0);
  68. self.contentViewFrame = CGRectMake(0, 0, JX_SCREEN_WIDTH,JX_SCREEN_HEIGHT - 50-0);
  69. [self.line_View setHidden:YES];
  70. [self.line_View removeFromSuperview];
  71. // [self.tabBar removeFromSuperview];
  72. }
  73. }
  74. - (void)awakeFromNib {
  75. [super awakeFromNib];
  76. _selectedControllerIndex = -1;
  77. _tabBar = [[YPTabBar alloc] init];
  78. _tabBar.delegate = self;
  79. [self.view addSubview:self.tabBar];
  80. }
  81. - (void)viewDidLoad {
  82. [super viewDidLoad];
  83. // 设置默认的tabBar frame
  84. CGSize screenSize = [UIScreen mainScreen].bounds.size;
  85. CGFloat navigationAndStatusBarHeight = 0;
  86. if (self.navigationController) {
  87. navigationAndStatusBarHeight = self.navigationController.navigationBar.frame.size.height + 20;
  88. }
  89. //
  90. [self.view addSubview:self.tabBar];
  91. // 设置默认的contentViewFrame
  92. self.contentViewFrame = CGRectMake(0,0,screenSize.width,
  93. screenSize.height - TAB_BAR_HEIGHT-(JX_SCREEN_HEIGHT>=812?90:65));
  94. self.view.clipsToBounds = YES;
  95. self.view.backgroundColor = [UIColor whiteColor];
  96. }
  97. - (void)setViewControllers:(NSArray *)viewControllers {
  98. for (UIViewController *controller in self.viewControllers) {
  99. [controller removeFromParentViewController];
  100. [controller.view removeFromSuperview];
  101. }
  102. _viewControllers = [viewControllers copy];
  103. [_viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  104. [self addChildViewController:obj];
  105. }];
  106. NSMutableArray *items = [NSMutableArray array];
  107. for (UIViewController *controller in _viewControllers) {
  108. YPTabItem *item = [[YPTabItem alloc] init];
  109. item.image = controller.yp_tabItemImage;
  110. item.selectedImage = controller.yp_tabItemSelectedImage;
  111. item.title = controller.yp_tabItemTitle;
  112. [items addObject:item];
  113. }
  114. self.tabBar.items = items;
  115. if (_didViewAppeared) {
  116. NSLog(@"asdfasdf");
  117. _selectedControllerIndex = -1;
  118. self.tabBar.selectedItemIndex = 0;
  119. }
  120. // 更新scrollView的content size
  121. if (self.scrollView) {
  122. self.scrollView.contentSize = CGSizeMake(self.contentViewFrame.size.width * _viewControllers.count,
  123. self.contentViewFrame.size.height);
  124. }
  125. }
  126. - (void)setContentViewFrame:(CGRect)contentViewFrame {
  127. _contentViewFrame = contentViewFrame;
  128. [self updateContentViewsFrame];
  129. }
  130. - (void)setContentScrollEnabledAndTapSwitchAnimated:(BOOL)switchAnimated {
  131. if (!self.scrollView) {
  132. self.scrollView = [[UIScrollView alloc] initWithFrame:self.contentViewFrame];
  133. self.scrollView.pagingEnabled = YES;
  134. self.scrollView.showsHorizontalScrollIndicator = NO;
  135. self.scrollView.showsVerticalScrollIndicator = NO;
  136. self.scrollView.scrollsToTop = NO;
  137. self.scrollView.delegate = self.tabBar;
  138. [self.view insertSubview:self.scrollView belowSubview:self.tabBar];
  139. self.scrollView.contentSize = CGSizeMake(self.contentViewFrame.size.width * _viewControllers.count,
  140. self.contentViewFrame.size.height);
  141. }
  142. [self updateContentViewsFrame];
  143. self.contentSwitchAnimated = switchAnimated;
  144. }
  145. - (void)updateContentViewsFrame {
  146. if (!_didViewAppeared) {
  147. return;
  148. }
  149. if (self.scrollView) {
  150. self.scrollView.frame = self.contentViewFrame;
  151. self.scrollView.contentSize = CGSizeMake(self.contentViewFrame.size.width * _viewControllers.count,
  152. self.contentViewFrame.size.height);
  153. [self.viewControllers enumerateObjectsUsingBlock:^(UIViewController * _Nonnull controller,
  154. NSUInteger idx, BOOL * _Nonnull stop) {
  155. if (controller.isViewLoaded) {
  156. controller.view.frame = CGRectMake(idx * self.contentViewFrame.size.width,
  157. 0,
  158. self.contentViewFrame.size.width,
  159. self.contentViewFrame.size.height);
  160. }
  161. }];
  162. [self.scrollView scrollRectToVisible:self.selectedController.view.frame animated:NO];
  163. } else {
  164. //self.selectedController.view.frame = CGRectMake(0,44, JX_SCREEN_HEIGHT-JX_SCREEN_TOP-70, 0);
  165. self.selectedController.view.frame = self.contentViewFrame;
  166. }
  167. }
  168. - (void)setSelectedControllerIndex:(NSInteger)selectedControllerIndex {
  169. UIViewController *oldController = nil;
  170. if (_selectedControllerIndex >= 0) {
  171. oldController = self.viewControllers[_selectedControllerIndex];
  172. }
  173. UIViewController *curController = self.viewControllers[selectedControllerIndex];
  174. BOOL isAppearFirstTime = YES;
  175. if (self.scrollView) {
  176. // contentView支持滚动
  177. // 调用oldController的viewWillDisappear方法
  178. [oldController viewWillDisappear:NO];
  179. if (!curController.view.superview) {
  180. // superview为空,表示为第一次加载,设置frame,并添加到scrollView
  181. curController.view.frame = CGRectMake(selectedControllerIndex * self.scrollView.frame.size.width,
  182. 0,
  183. self.scrollView.frame.size.width,
  184. self.scrollView.frame.size.height);
  185. [self.scrollView addSubview:curController.view];
  186. } else {
  187. // superview不为空,表示为已经加载过了,调用viewWillAppear方法
  188. isAppearFirstTime = NO;
  189. [curController viewWillAppear:NO];
  190. }
  191. // 切换到curController
  192. [self.scrollView scrollRectToVisible:curController.view.frame animated:self.contentSwitchAnimated];
  193. } else {
  194. // contentView不支持滚动
  195. // 将oldController的view移除
  196. if (oldController) {
  197. [oldController.view removeFromSuperview];
  198. }
  199. [self.view insertSubview:curController.view belowSubview:self.tabBar];
  200. // 设置curController.view的frame
  201. if (!CGRectEqualToRect(curController.view.frame, self.contentViewFrame)) {
  202. curController.view.frame = self.contentViewFrame;
  203. }
  204. }
  205. // 当contentView为scrollView及其子类时,设置它支持点击状态栏回到顶部
  206. if (oldController && [oldController.view isKindOfClass:[UIScrollView class]]) {
  207. [(UIScrollView *)oldController.view setScrollsToTop:NO];
  208. }
  209. if ([curController.view isKindOfClass:[UIScrollView class]]) {
  210. [(UIScrollView *)curController.view setScrollsToTop:YES];
  211. }
  212. _selectedControllerIndex = selectedControllerIndex;
  213. // 调用状态切换的回调方法
  214. [oldController tabItemDidDeselected];
  215. [curController tabItemDidSelected];
  216. if (self.scrollView) {
  217. [oldController viewDidDisappear:NO];
  218. if (!isAppearFirstTime) {
  219. [curController viewDidAppear:NO];
  220. }
  221. }
  222. }
  223. - (UIViewController *)selectedController {
  224. if (self.selectedControllerIndex >= 0) {
  225. return self.viewControllers[self.selectedControllerIndex];
  226. }
  227. return nil;
  228. }
  229. #pragma mark - YPTabBarDelegate
  230. - (void)yp_tabBar:(YPTabBar *)tabBar didSelectedItemAtIndex:(NSInteger)index {
  231. if (index == self.selectedControllerIndex) {
  232. return;
  233. }
  234. self.selectedControllerIndex = index;
  235. }
  236. @end
  237. @implementation UIViewController (YPTabBarController)
  238. - (NSString *)yp_tabItemTitle {
  239. return objc_getAssociatedObject(self, _cmd);
  240. }
  241. - (void)setYp_tabItemTitle:(NSString *)yp_tabItemTitle {
  242. objc_setAssociatedObject(self, @selector(yp_tabItemTitle), yp_tabItemTitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
  243. }
  244. - (UIImage *)yp_tabItemImage {
  245. return objc_getAssociatedObject(self, _cmd);
  246. }
  247. - (void)setYp_tabItemImage:(UIImage *)yp_tabItemImage {
  248. objc_setAssociatedObject(self, @selector(yp_tabItemImage), yp_tabItemImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  249. }
  250. - (UIImage *)yp_tabItemSelectedImage {
  251. return objc_getAssociatedObject(self, _cmd);
  252. }
  253. - (void)setYp_tabItemSelectedImage:(UIImage *)yp_tabItemSelectedImage {
  254. objc_setAssociatedObject(self, @selector(yp_tabItemSelectedImage), yp_tabItemSelectedImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  255. }
  256. - (YPTabItem *)yp_tabItem {
  257. YPTabBar *tabBar = self.yp_tabBarController.tabBar;
  258. NSInteger index = [self.yp_tabBarController.viewControllers indexOfObject:self];
  259. return tabBar.items[index];
  260. }
  261. - (YPTabBarController *)yp_tabBarController {
  262. return (YPTabBarController *)self.parentViewController;
  263. }
  264. - (void)tabItemDidSelected {
  265. }
  266. - (void)tabItemDidDeselected {
  267. }
  268. @end