You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.1 KiB

  1. //
  2. // CommonTabBar.m
  3. // HC
  4. //
  5. // Created by huilinLi on 2025/11/25.
  6. //
  7. #import "CommonTabBar.h"
  8. @interface CommonTabBar ()
  9. @property (nonatomic, strong) NSArray<UIButton *> *tabButtons;
  10. @end
  11. @implementation CommonTabBar
  12. - (instancetype)initWithTabItems:(NSArray<NSDictionary *> *)tabItems {
  13. self = [super init];
  14. if (self) {
  15. self.backgroundColor = [UIColor blackColor];
  16. self.translatesAutoresizingMaskIntoConstraints = NO;
  17. [self setupTabButtonsWithItems:tabItems];
  18. }
  19. return self;
  20. }
  21. - (void)setupTabButtonsWithItems:(NSArray<NSDictionary *> *)tabItems {
  22. NSMutableArray<UIButton *> *buttons = [NSMutableArray array];
  23. for (NSInteger i = 0; i < tabItems.count; i++) {
  24. NSDictionary *item = tabItems[i];
  25. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  26. UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration];
  27. config.image = [UIImage systemImageNamed:item[@"icon"] ?: @""];
  28. config.title = item[@"title"];
  29. config.imagePlacement = NSDirectionalRectEdgeTop;
  30. config.imagePadding = 5;
  31. btn.configuration = config;
  32. BOOL selected = [item[@"selected"] boolValue];
  33. btn.tintColor = selected ? [UIColor blueColor] : [UIColor whiteColor];
  34. btn.titleLabel.font = [UIFont systemFontOfSize:10];
  35. btn.tag = i;
  36. [btn addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  37. [self addSubview:btn];
  38. [buttons addObject:btn];
  39. }
  40. self.tabButtons = buttons;
  41. [self setupTabButtonConstraints];
  42. }
  43. - (void)setupTabButtonConstraints {
  44. for (NSInteger i = 0; i < self.tabButtons.count; i++) {
  45. UIButton *btn = self.tabButtons[i];
  46. btn.translatesAutoresizingMaskIntoConstraints = NO;
  47. [NSLayoutConstraint activateConstraints:@[
  48. [btn.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:1.0/self.tabButtons.count],
  49. [btn.heightAnchor constraintEqualToAnchor:self.heightAnchor],
  50. [btn.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
  51. (i == 0) ? [btn.leadingAnchor constraintEqualToAnchor:self.leadingAnchor] :
  52. [btn.leadingAnchor constraintEqualToAnchor:self.tabButtons[i-1].trailingAnchor],
  53. ]];
  54. }
  55. // TabBar自身高度
  56. [self.heightAnchor constraintEqualToConstant:60].active = YES;
  57. }
  58. - (void)tabButtonTapped:(UIButton *)sender {
  59. self.selectedIndex = sender.tag;
  60. for (UIButton *btn in self.tabButtons) {
  61. btn.tintColor = (btn.tag == sender.tag) ? [UIColor blueColor] : [UIColor whiteColor];
  62. }
  63. if ([self.delegate respondsToSelector:@selector(tabBarDidSelectIndex:)]) {
  64. [self.delegate tabBarDidSelectIndex:sender.tag];
  65. }
  66. }
  67. // 外部设置选中索引时更新样式
  68. - (void)setSelectedIndex:(NSInteger)selectedIndex {
  69. _selectedIndex = selectedIndex;
  70. for (UIButton *btn in self.tabButtons) {
  71. btn.tintColor = (btn.tag == selectedIndex) ? [UIColor blueColor] : [UIColor whiteColor];
  72. }
  73. }
  74. @end