CustomTabbarView.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // CustomTabbarView.m
  3. // BuguLive
  4. //
  5. // Created by qitewei on 2025/8/27.
  6. // Copyright © 2025 xfg. All rights reserved.
  7. //
  8. #import "CustomTabbarView.h"
  9. @interface CustomTabbarView()
  10. @property (nonatomic, strong) NSMutableArray<UIButton *> *tabBarButtons;
  11. @end
  12. @implementation CustomTabbarView
  13. - (instancetype)initWithFrame:(CGRect)frame
  14. {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. [self setupView];
  18. }
  19. return self;
  20. }
  21. - (void)setupView {
  22. self.backgroundColor = [UIColor whiteColor];
  23. // 添加顶部分割线
  24. UIView *topLine = [[UIView alloc] init];
  25. topLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
  26. [self addSubview:topLine];
  27. topLine.translatesAutoresizingMaskIntoConstraints = NO;
  28. [NSLayoutConstraint activateConstraints:@[
  29. [topLine.topAnchor constraintEqualToAnchor:self.topAnchor],
  30. [topLine.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
  31. [topLine.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
  32. [topLine.heightAnchor constraintEqualToConstant:0.5]
  33. ]];
  34. [self addSubview:self.stackView];
  35. self.stackView.translatesAutoresizingMaskIntoConstraints = NO;
  36. [NSLayoutConstraint activateConstraints:@[
  37. [self.stackView.topAnchor constraintEqualToAnchor:self.topAnchor constant:8],
  38. [self.stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
  39. [self.stackView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
  40. [self.stackView.bottomAnchor constraintEqualToAnchor:self.safeAreaLayoutGuide.bottomAnchor constant:-8]
  41. ]];
  42. }
  43. - (UIStackView *)stackView {
  44. if (!_stackView) {
  45. _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]];
  46. _stackView.axis = UILayoutConstraintAxisHorizontal;
  47. _stackView.distribution = UIStackViewDistributionFillEqually;
  48. _stackView.spacing = 0;
  49. _stackView.alignment = UIStackViewAlignmentCenter;
  50. }
  51. return _stackView;
  52. }
  53. - (NSMutableArray<UIButton *> *)tabBarButtons {
  54. if (!_tabBarButtons) {
  55. _tabBarButtons = [NSMutableArray array];
  56. }
  57. return _tabBarButtons;
  58. }
  59. - (void)addTabBarItemWithTitle:(NSString *)title
  60. normalImage:(NSString *)normalImage
  61. selectedImage:(NSString *)selectedImage {
  62. QMUIButton *button = [QMUIButton buttonWithType:UIButtonTypeCustom];
  63. button.imagePosition = QMUIButtonImagePositionTop;
  64. button.tag = self.tabBarButtons.count;
  65. // 设置图片
  66. UIImage *normalImg = [UIImage imageNamed:normalImage];
  67. UIImage *selectedImg = [UIImage imageNamed:selectedImage];
  68. if (normalImg) {
  69. [button setImage:normalImg forState:UIControlStateNormal];
  70. }
  71. if (selectedImg) {
  72. [button setImage:selectedImg forState:UIControlStateSelected];
  73. }
  74. // 设置标题
  75. [button setTitle:title forState:UIControlStateNormal];
  76. [button setTitleColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0] forState:UIControlStateNormal];
  77. [button setTitleColor:[UIColor colorWithRed:75.0/255.0 green:200.0/255.0 blue:252.0/255.0 alpha:1.0] forState:UIControlStateSelected];
  78. button.titleLabel.font = [UIFont systemFontOfSize:10];
  79. // 添加点击事件
  80. [button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  81. // 添加到数组和stackView
  82. [self.tabBarButtons addObject:button];
  83. [self.stackView addArrangedSubview:button];
  84. // 如果是第一个按钮,设置为选中状态
  85. if (self.tabBarButtons.count == 1) {
  86. [self setSelectedIndex:0 animated:NO];
  87. }
  88. }
  89. - (void)tabBarButtonClicked:(UIButton *)sender {
  90. NSInteger index = sender.tag;
  91. [self setSelectedIndex:index animated:YES];
  92. if (self.delegate && [self.delegate respondsToSelector:@selector(customTabbarView:didSelectItemAtIndex:)]) {
  93. [self.delegate customTabbarView:self didSelectItemAtIndex:index];
  94. }
  95. }
  96. - (void)setSelectedIndex:(NSInteger)selectedIndex {
  97. [self setSelectedIndex:selectedIndex animated:NO];
  98. }
  99. - (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated {
  100. if (selectedIndex < 0 || selectedIndex >= self.tabBarButtons.count) {
  101. return;
  102. }
  103. _selectedIndex = selectedIndex;
  104. // 更新按钮状态
  105. for (NSInteger i = 0; i < self.tabBarButtons.count; i++) {
  106. UIButton *button = self.tabBarButtons[i];
  107. button.selected = (i == selectedIndex);
  108. if (animated) {
  109. [UIView animateWithDuration:0.2 animations:^{
  110. button.transform = (i == selectedIndex) ? CGAffineTransformMakeScale(1.1, 1.1) : CGAffineTransformIdentity;
  111. } completion:^(BOOL finished) {
  112. if (i == selectedIndex) {
  113. [UIView animateWithDuration:0.1 animations:^{
  114. button.transform = CGAffineTransformIdentity;
  115. }];
  116. }
  117. }];
  118. }
  119. }
  120. }
  121. @end