YBTabBar.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // YBTabBar.m
  3. // WaWaJiClient
  4. //
  5. // Created by Rookie on 2017/11/15.
  6. // Copyright © 2017年 zego. All rights reserved.
  7. //
  8. #import "YBTabBar.h"
  9. @interface YBTabBar()
  10. @property (nonatomic,weak) UIView *addBgview;
  11. @property (nonatomic, weak) UIButton *addButton;
  12. @end
  13. @implementation YBTabBar
  14. - (instancetype)initWithFrame:(CGRect)frame{
  15. if(self=[super initWithFrame:frame]){
  16. UIView *addBgv = [[UIView alloc]init];
  17. addBgv.backgroundColor = [UIColor clearColor];
  18. [self addSubview:addBgv];
  19. self.addBgview = addBgv;
  20. UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
  21. CGRect frame = addButton.frame;
  22. //frame.size = addButton.currentBackgroundImage.size;
  23. frame.size = CGSizeMake(40, 40);
  24. addButton.frame = frame;
  25. //方式一 图片无文字
  26. [addButton setBackgroundImage:[UIImage imageNamed:@"tab_center"] forState:UIControlStateNormal];
  27. [addButton setBackgroundImage:[UIImage imageNamed:@"tab_center"] forState:UIControlStateHighlighted];
  28. //方式二 图片有文字
  29. /*
  30. [addButton setImage:[UIImage imageNamed:@"center"] forState:UIControlStateNormal];
  31. [addButton setImage:[UIImage imageNamed:@"center"] forState:UIControlStateHighlighted];
  32. [addButton setTitle:@"" forState: UIControlStateNormal];
  33. [addButton setTitle:@"" forState: UIControlStateHighlighted];
  34. [addButton setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
  35. [addButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
  36. addButton.titleLabel.font = SYS_Font(11);
  37. [addButton setImageEdgeInsets:UIEdgeInsetsMake(-(65-addButton.imageView.frame.size.height), 0.0, 0.0, -addButton.titleLabel.frame.size.height)];
  38. [addButton setTitleEdgeInsets:UIEdgeInsetsMake(-3, -addButton.imageView.frame.size.height, -(65-addButton.titleLabel.frame.size.height), 0.0)];
  39. */
  40. [addButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
  41. /** 预留*/
  42. [self addSubview:addButton];
  43. self.addButton = addButton;
  44. }
  45. return self;
  46. }
  47. /**
  48. * 中间按钮点击事件
  49. */
  50. - (void)publishClick{
  51. if ([_tabbarDelegate respondsToSelector:@selector(centerBtnDidClicked)]) {
  52. [_tabbarDelegate centerBtnDidClicked];
  53. }
  54. }
  55. - (void)layoutSubviews {
  56. [super layoutSubviews];
  57. CGFloat width = self.frame.size.width;
  58. CGFloat height = self.frame.size.height - ShowDiff;
  59. /**
  60. * 单独设置中间的按钮
  61. */
  62. self.addButton.center = CGPointMake(width * 0.5, height * 0.5);
  63. self.addBgview.frame = CGRectMake(width*2/5, 0, width/5, height);
  64. CGFloat buttonY = 0;
  65. CGFloat buttonW = width / 5;
  66. CGFloat buttonH = height;
  67. NSInteger index = 0;
  68. for (UIControl *button in self.subviews) {
  69. if (![button isKindOfClass:[UIControl class]] || button == self.addButton) continue;
  70. /** 中间空出 */
  71. CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
  72. /** 中间不空 */
  73. // CGFloat buttonX = buttonW * index;
  74. button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
  75. index++;
  76. }
  77. }
  78. @end