| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- //
- // 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<UIButton *> *tabBarButtons;
- @end
- @implementation CustomTabbarView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self setupView];
- }
- return self;
- }
- - (void)setupView {
- self.backgroundColor = [UIColor whiteColor];
-
- [self addSubview:self.stackView];
- self.stackView.translatesAutoresizingMaskIntoConstraints = NO;
- [NSLayoutConstraint activateConstraints:@[
- [self.stackView.topAnchor constraintEqualToAnchor:self.topAnchor constant:0],
- [self.stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
- [self.stackView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
- [self.stackView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:0]
- ]];
- }
- - (UIStackView *)stackView {
- if (!_stackView) {
- _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]];
- _stackView.axis = UILayoutConstraintAxisHorizontal;
- _stackView.distribution = UIStackViewDistributionFillEqually;
- _stackView.spacing = 0;
- _stackView.alignment = UIStackViewAlignmentCenter;
- }
- return _stackView;
- }
- - (NSMutableArray<UIButton *> *)tabBarButtons {
- if (!_tabBarButtons) {
- _tabBarButtons = [NSMutableArray array];
- }
- return _tabBarButtons;
- }
- - (void)addTabBarItemWithTitle:(NSString *)title
- normalImage:(NSString *)normalImage
- selectedImage:(NSString *)selectedImage {
-
- UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/4, self.height)];
- container.translatesAutoresizingMaskIntoConstraints = NO;
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- 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 addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
-
- UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
- titleButton.titleLabel.font = [UIFont systemFontOfSize:10];
- // 设置标题
- [titleButton setTitle:title forState:UIControlStateNormal];
- [titleButton setTitleColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0] forState:UIControlStateNormal];
- [titleButton setTitleColor:[UIColor colorWithRed:75.0/255.0 green:200.0/255.0 blue:252.0/255.0 alpha:1.0] forState:UIControlStateSelected];
-
- [container addSubview:button];
- [container addSubview:titleButton];
-
- // 添加到数组和stackView
- [self.tabBarButtons addObject:button];
- [self.stackView addArrangedSubview:container];
-
- [container.widthAnchor constraintEqualToConstant:container.size.width].active = YES;
- [container.heightAnchor constraintEqualToConstant:container.size.height].active = YES;
-
-
- BOOL isFirst = self.tabBarButtons.count == 1;
-
- [button mas_makeConstraints:^(MASConstraintMaker *make) {
- make.size.mas_equalTo(28);
- make.top.mas_offset(isFirst ? 10 : 16);
- make.centerX.mas_offset(0);
- }];
-
- [titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(button.mas_bottom).mas_offset(6);
- make.centerX.mas_offset(0);
- }];
-
- // 如果是第一个按钮,设置为选中状态
- if (isFirst) {
- [self setSelectedIndex:0 animated:NO];
- }
- }
- - (void)tabBarButtonClicked:(UIButton *)sender {
- if (sender.isSelected) {
- return;
- }
- 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);
- [button mas_updateConstraints:^(MASConstraintMaker *make) {
- make.size.mas_equalTo(button.isSelected ? 60 : 28);
- make.top.mas_offset(button.isSelected ? 10 : 16);
- }];
- }
- }
- @end
|