| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // 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];
-
- // 添加顶部分割线
- 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<UIButton *> *)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
|