// // CustomTabbarView.m // BuguLive // // Created by qitewei on 2025/8/27. // Copyright © 2025 xfg. All rights reserved. // #import "CustomTabbarView.h" @interface CustomTabbarView() @property (nonatomic, strong) NSMutableArray *tabBarButtons; @end @implementation CustomTabbarView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setupView]; } return self; } - (void)setupView { self.backgroundColor = [UIColor whiteColor]; // 添加顶部分割线 UIView *topLine = [[UIView alloc] init]; topLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]; [self addSubview:topLine]; topLine.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [topLine.topAnchor constraintEqualToAnchor:self.topAnchor], [topLine.leadingAnchor constraintEqualToAnchor:self.leadingAnchor], [topLine.trailingAnchor constraintEqualToAnchor:self.trailingAnchor], [topLine.heightAnchor constraintEqualToConstant:0.5] ]]; [self addSubview:self.stackView]; self.stackView.translatesAutoresizingMaskIntoConstraints = NO; [NSLayoutConstraint activateConstraints:@[ [self.stackView.topAnchor constraintEqualToAnchor:self.topAnchor constant:8], [self.stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor], [self.stackView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor], [self.stackView.bottomAnchor constraintEqualToAnchor:self.safeAreaLayoutGuide.bottomAnchor constant:-8] ]]; } - (UIStackView *)stackView { if (!_stackView) { _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]]; _stackView.axis = UILayoutConstraintAxisHorizontal; _stackView.distribution = UIStackViewDistributionFillEqually; _stackView.spacing = 0; _stackView.alignment = UIStackViewAlignmentCenter; } return _stackView; } - (NSMutableArray *)tabBarButtons { if (!_tabBarButtons) { _tabBarButtons = [NSMutableArray array]; } return _tabBarButtons; } - (void)addTabBarItemWithTitle:(NSString *)title normalImage:(NSString *)normalImage selectedImage:(NSString *)selectedImage { QMUIButton *button = [QMUIButton buttonWithType:UIButtonTypeCustom]; button.imagePosition = QMUIButtonImagePositionTop; button.tag = self.tabBarButtons.count; // 设置图片 UIImage *normalImg = [UIImage imageNamed:normalImage]; UIImage *selectedImg = [UIImage imageNamed:selectedImage]; if (normalImg) { [button setImage:normalImg forState:UIControlStateNormal]; } if (selectedImg) { [button setImage:selectedImg forState:UIControlStateSelected]; } // 设置标题 [button setTitle:title forState:UIControlStateNormal]; [button setTitleColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0] forState:UIControlStateNormal]; [button setTitleColor:[UIColor colorWithRed:75.0/255.0 green:200.0/255.0 blue:252.0/255.0 alpha:1.0] forState:UIControlStateSelected]; button.titleLabel.font = [UIFont systemFontOfSize:10]; // 添加点击事件 [button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; // 添加到数组和stackView [self.tabBarButtons addObject:button]; [self.stackView addArrangedSubview:button]; // 如果是第一个按钮,设置为选中状态 if (self.tabBarButtons.count == 1) { [self setSelectedIndex:0 animated:NO]; } } - (void)tabBarButtonClicked:(UIButton *)sender { NSInteger index = sender.tag; [self setSelectedIndex:index animated:YES]; if (self.delegate && [self.delegate respondsToSelector:@selector(customTabbarView:didSelectItemAtIndex:)]) { [self.delegate customTabbarView:self didSelectItemAtIndex:index]; } } - (void)setSelectedIndex:(NSInteger)selectedIndex { [self setSelectedIndex:selectedIndex animated:NO]; } - (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated { if (selectedIndex < 0 || selectedIndex >= self.tabBarButtons.count) { return; } _selectedIndex = selectedIndex; // 更新按钮状态 for (NSInteger i = 0; i < self.tabBarButtons.count; i++) { UIButton *button = self.tabBarButtons[i]; button.selected = (i == selectedIndex); if (animated) { [UIView animateWithDuration:0.2 animations:^{ button.transform = (i == selectedIndex) ? CGAffineTransformMakeScale(1.1, 1.1) : CGAffineTransformIdentity; } completion:^(BOOL finished) { if (i == selectedIndex) { [UIView animateWithDuration:0.1 animations:^{ button.transform = CGAffineTransformIdentity; }]; } }]; } } } @end