MJTabBar.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // XMGTabBar.m
  3. // 备课-百思不得姐
  4. //
  5. // Created by MJ Lee on 15/6/15.
  6. // Copyright © 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJTabBar.h"
  9. #import "Masonry.h"
  10. #import "UIView+LK.h"
  11. @interface MJTabBar()
  12. @property (nonatomic,strong) NSMutableArray *tabBarButtonArray;
  13. @property (nonatomic,weak) UIButton *publishButton;
  14. @end
  15. @implementation MJTabBar
  16. - (nonnull instancetype)initWithFrame:(CGRect)frame
  17. {
  18. if (self = [super initWithFrame:frame]) {
  19. _tabBarButtonArray=[NSMutableArray array];
  20. UIButton *publishButton = [[UIButton alloc] init];
  21. [publishButton setTitle:@"福利" forState:UIControlStateNormal];
  22. [publishButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
  23. [publishButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
  24. [self addSubview:publishButton];
  25. self.publishButton=publishButton;
  26. publishButton.backgroundColor=[UIColor greenColor];
  27. [publishButton mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.center.equalTo(self);
  29. make.size.mas_equalTo(CGSizeMake(JX_SCREEN_WIDTH/3, JX_SCREEN_HEIGHT>=812?64:49));
  30. }];
  31. [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
  32. }
  33. return self;
  34. }
  35. - (void)publishClick
  36. {
  37. }
  38. - (void)layoutSubviews
  39. {
  40. [super layoutSubviews];
  41. // 把 tabBarButton 取出来(把 tabBar 的 subViews 打印出来就明白了)
  42. NSMutableArray *tabBarButtonArray = [NSMutableArray array];
  43. for (UIView *view in self.subviews) {
  44. if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  45. [tabBarButtonArray addObject:view];
  46. [view removeFromSuperview];
  47. }
  48. }
  49. CGFloat barWidth = self.bounds.size.width;
  50. CGFloat barHeight = self.bounds.size.height;
  51. CGFloat centerBtnWidth = CGRectGetWidth(self.publishButton.frame);
  52. CGFloat centerBtnHeight = CGRectGetHeight(self.publishButton.frame);
  53. // 设置中间按钮的位置,居中,凸起一丢丢
  54. self.publishButton.center = CGPointMake(barWidth / 2, barHeight - centerBtnHeight/2 - 5);
  55. // 重新布局其他 tabBarItem
  56. // 平均分配其他 tabBarItem 的宽度
  57. CGFloat barItemWidth = (barWidth - centerBtnWidth) / tabBarButtonArray.count;
  58. // 逐个布局 tabBarItem,修改 UITabBarButton 的 frame
  59. [tabBarButtonArray enumerateObjectsUsingBlock:^(UIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
  60. CGRect frame = view.frame;
  61. if (idx >= tabBarButtonArray.count / 2) {
  62. // 重新设置 x 坐标,如果排在中间按钮的右边需要加上中间按钮的宽度
  63. frame.origin.x = idx * barItemWidth + centerBtnWidth;
  64. } else {
  65. frame.origin.x = idx * barItemWidth;
  66. }
  67. // 重新设置宽度
  68. frame.size.width = barItemWidth;
  69. view.frame = frame;
  70. }];
  71. // 把中间按钮带到视图最前面
  72. [self bringSubviewToFront:self.publishButton];
  73. }
  74. @end