处理tabbar
This commit is contained in:
32
keyBoard/Class/Base/V/KBCustomTabBar.h
Normal file
32
keyBoard/Class/Base/V/KBCustomTabBar.h
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// KBCustomTabBar.h
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Assistant on 2026/01/16.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class KBCustomTabBar;
|
||||
|
||||
@protocol KBCustomTabBarDelegate <NSObject>
|
||||
|
||||
- (void)customTabBar:(KBCustomTabBar *)tabBar
|
||||
didSelectItemAtIndex:(NSInteger)index;
|
||||
|
||||
@end
|
||||
|
||||
@interface KBCustomTabBar : UIView
|
||||
|
||||
@property(nonatomic, weak) id<KBCustomTabBarDelegate> delegate;
|
||||
@property(nonatomic, assign) NSInteger selectedIndex;
|
||||
|
||||
- (instancetype)initWithItems:(NSArray<UITabBarItem *> *)items;
|
||||
- (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated;
|
||||
- (void)setTransparentBackground:(BOOL)transparent;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
159
keyBoard/Class/Base/V/KBCustomTabBar.m
Normal file
159
keyBoard/Class/Base/V/KBCustomTabBar.m
Normal file
@@ -0,0 +1,159 @@
|
||||
//
|
||||
// KBCustomTabBar.m
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Assistant on 2026/01/16.
|
||||
//
|
||||
|
||||
#import "KBCustomTabBar.h"
|
||||
#import "Masonry.h"
|
||||
|
||||
@interface KBCustomTabBar ()
|
||||
|
||||
@property(nonatomic, strong) NSArray<UITabBarItem *> *items;
|
||||
@property(nonatomic, strong) NSMutableArray<UIButton *> *buttons;
|
||||
@property(nonatomic, strong) UIView *backgroundView;
|
||||
|
||||
@end
|
||||
|
||||
@implementation KBCustomTabBar
|
||||
|
||||
- (instancetype)initWithItems:(NSArray<UITabBarItem *> *)items {
|
||||
self = [super initWithFrame:CGRectZero];
|
||||
if (self) {
|
||||
_items = items;
|
||||
_selectedIndex = 0;
|
||||
_buttons = [NSMutableArray array];
|
||||
[self setupUI];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setupUI {
|
||||
// 背景视图
|
||||
self.backgroundView = [[UIView alloc] init];
|
||||
self.backgroundView.backgroundColor = [UIColor whiteColor];
|
||||
[self addSubview:self.backgroundView];
|
||||
[self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.edges.equalTo(self);
|
||||
}];
|
||||
|
||||
// 创建按钮
|
||||
UIStackView *stackView = [[UIStackView alloc] init];
|
||||
stackView.axis = UILayoutConstraintAxisHorizontal;
|
||||
stackView.distribution = UIStackViewDistributionFillEqually;
|
||||
stackView.alignment = UIStackViewAlignmentFill;
|
||||
[self addSubview:stackView];
|
||||
[stackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.edges.equalTo(self);
|
||||
}];
|
||||
|
||||
for (NSInteger i = 0; i < self.items.count; i++) {
|
||||
UITabBarItem *item = self.items[i];
|
||||
UIButton *button = [self createButtonWithItem:item index:i];
|
||||
[stackView addArrangedSubview:button];
|
||||
[self.buttons addObject:button];
|
||||
}
|
||||
|
||||
// 设置初始选中状态
|
||||
[self updateButtonStates];
|
||||
}
|
||||
|
||||
- (UIButton *)createButtonWithItem:(UITabBarItem *)item index:(NSInteger)index {
|
||||
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
button.tag = index;
|
||||
|
||||
// 设置图片
|
||||
[button setImage:item.image forState:UIControlStateNormal];
|
||||
[button setImage:item.selectedImage forState:UIControlStateSelected];
|
||||
|
||||
// 设置标题
|
||||
[button setTitle:item.title forState:UIControlStateNormal];
|
||||
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
|
||||
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
|
||||
button.titleLabel.font = [UIFont systemFontOfSize:10];
|
||||
|
||||
// 布局:图片在上,文字在下
|
||||
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
button.titleLabel.textAlignment = NSTextAlignmentCenter;
|
||||
|
||||
// 调整图片和文字位置
|
||||
CGFloat spacing = 4.0;
|
||||
button.imageEdgeInsets = UIEdgeInsetsMake(-spacing, 0, spacing, 0);
|
||||
button.titleEdgeInsets = UIEdgeInsetsMake(
|
||||
spacing, -button.imageView.frame.size.width, -spacing, 0);
|
||||
button.contentEdgeInsets = UIEdgeInsetsMake(spacing, 0, spacing, 0);
|
||||
|
||||
// 垂直排列
|
||||
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
|
||||
[button
|
||||
setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
|
||||
|
||||
[button addTarget:self
|
||||
action:@selector(buttonTapped:)
|
||||
forControlEvents:UIControlEventTouchUpInside];
|
||||
|
||||
return button;
|
||||
}
|
||||
|
||||
- (void)buttonTapped:(UIButton *)button {
|
||||
NSInteger index = button.tag;
|
||||
if (index != self.selectedIndex) {
|
||||
self.selectedIndex = index;
|
||||
[self updateButtonStates];
|
||||
|
||||
if ([self.delegate respondsToSelector:@selector(customTabBar:
|
||||
didSelectItemAtIndex:)]) {
|
||||
[self.delegate customTabBar:self didSelectItemAtIndex:index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setSelectedIndex:(NSInteger)selectedIndex animated:(BOOL)animated {
|
||||
if (_selectedIndex != selectedIndex) {
|
||||
_selectedIndex = selectedIndex;
|
||||
[self updateButtonStates];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setSelectedIndex:(NSInteger)selectedIndex {
|
||||
[self setSelectedIndex:selectedIndex animated:NO];
|
||||
}
|
||||
|
||||
- (void)updateButtonStates {
|
||||
for (NSInteger i = 0; i < self.buttons.count; i++) {
|
||||
UIButton *button = self.buttons[i];
|
||||
button.selected = (i == self.selectedIndex);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setTransparentBackground:(BOOL)transparent {
|
||||
if (transparent) {
|
||||
self.backgroundView.backgroundColor = [UIColor clearColor];
|
||||
} else {
|
||||
self.backgroundView.backgroundColor = [UIColor whiteColor];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)layoutSubviews {
|
||||
[super layoutSubviews];
|
||||
|
||||
// 调整按钮的图片和文字布局
|
||||
for (UIButton *button in self.buttons) {
|
||||
CGFloat imageWidth = button.imageView.frame.size.width;
|
||||
CGFloat imageHeight = button.imageView.frame.size.height;
|
||||
CGFloat titleWidth = button.titleLabel.frame.size.width;
|
||||
CGFloat titleHeight = button.titleLabel.frame.size.height;
|
||||
|
||||
CGFloat spacing = 4.0;
|
||||
|
||||
// 图片在上
|
||||
button.imageEdgeInsets =
|
||||
UIEdgeInsetsMake(-(titleHeight + spacing), 0, 0, -titleWidth);
|
||||
// 文字在下
|
||||
button.titleEdgeInsets =
|
||||
UIEdgeInsetsMake(0, -imageWidth, -(imageHeight + spacing), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user