CustomTabbarView.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. [self addSubview:self.stackView];
  24. self.stackView.translatesAutoresizingMaskIntoConstraints = NO;
  25. [NSLayoutConstraint activateConstraints:@[
  26. [self.stackView.topAnchor constraintEqualToAnchor:self.topAnchor constant:0],
  27. [self.stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
  28. [self.stackView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
  29. [self.stackView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:0]
  30. ]];
  31. }
  32. - (UIStackView *)stackView {
  33. if (!_stackView) {
  34. _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]];
  35. _stackView.axis = UILayoutConstraintAxisHorizontal;
  36. _stackView.distribution = UIStackViewDistributionFillEqually;
  37. _stackView.spacing = 0;
  38. _stackView.alignment = UIStackViewAlignmentCenter;
  39. }
  40. return _stackView;
  41. }
  42. - (NSMutableArray<UIButton *> *)tabBarButtons {
  43. if (!_tabBarButtons) {
  44. _tabBarButtons = [NSMutableArray array];
  45. }
  46. return _tabBarButtons;
  47. }
  48. - (void)addTabBarItemWithTitle:(NSString *)title
  49. normalImage:(NSString *)normalImage
  50. selectedImage:(NSString *)selectedImage {
  51. UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/4, self.height)];
  52. container.translatesAutoresizingMaskIntoConstraints = NO;
  53. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  54. button.tag = self.tabBarButtons.count;
  55. // 设置图片
  56. UIImage *normalImg = [UIImage imageNamed:normalImage];
  57. UIImage *selectedImg = [UIImage imageNamed:selectedImage];
  58. if (normalImg) {
  59. [button setImage:normalImg forState:UIControlStateNormal];
  60. }
  61. if (selectedImg) {
  62. [button setImage:selectedImg forState:UIControlStateSelected];
  63. }
  64. // 添加点击事件
  65. [button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  66. UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
  67. titleButton.titleLabel.font = [UIFont systemFontOfSize:10];
  68. // 设置标题
  69. [titleButton setTitle:title forState:UIControlStateNormal];
  70. [titleButton setTitleColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0] forState:UIControlStateNormal];
  71. [titleButton setTitleColor:[UIColor colorWithRed:75.0/255.0 green:200.0/255.0 blue:252.0/255.0 alpha:1.0] forState:UIControlStateSelected];
  72. [container addSubview:button];
  73. [container addSubview:titleButton];
  74. // 添加到数组和stackView
  75. [self.tabBarButtons addObject:button];
  76. [self.stackView addArrangedSubview:container];
  77. [container.widthAnchor constraintEqualToConstant:container.size.width].active = YES;
  78. [container.heightAnchor constraintEqualToConstant:container.size.height].active = YES;
  79. BOOL isFirst = self.tabBarButtons.count == 1;
  80. [button mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.size.mas_equalTo(28);
  82. make.top.mas_offset(isFirst ? 10 : 16);
  83. make.centerX.mas_offset(0);
  84. }];
  85. [titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
  86. make.top.equalTo(button.mas_bottom).mas_offset(6);
  87. make.centerX.mas_offset(0);
  88. }];
  89. // 如果是第一个按钮,设置为选中状态
  90. if (isFirst) {
  91. [self setSelectedIndex:0 animated:NO];
  92. }
  93. }
  94. - (void)tabBarButtonClicked:(UIButton *)sender {
  95. if (sender.isSelected) {
  96. return;
  97. }
  98. NSInteger index = sender.tag;
  99. [self setSelectedIndex:index animated:YES];
  100. if (self.delegate && [self.delegate respondsToSelector:@selector(customTabbarView:didSelectItemAtIndex:)]) {
  101. [self.delegate customTabbarView:self didSelectItemAtIndex:index];
  102. }
  103. }
  104. - (void)setSelectedIndex:(NSInteger)selectedIndex {
  105. [self setSelectedIndex:selectedIndex animated:NO];
  106. }
  107. - (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated {
  108. if (selectedIndex < 0 || selectedIndex >= self.tabBarButtons.count) {
  109. return;
  110. }
  111. _selectedIndex = selectedIndex;
  112. // 更新按钮状态
  113. for (NSInteger i = 0; i < self.tabBarButtons.count; i++) {
  114. UIButton *button = self.tabBarButtons[i];
  115. button.selected = (i == selectedIndex);
  116. [button mas_updateConstraints:^(MASConstraintMaker *make) {
  117. make.size.mas_equalTo(button.isSelected ? 60 : 28);
  118. make.top.mas_offset(button.isSelected ? 10 : 16);
  119. }];
  120. }
  121. }
  122. @end