|
|
@@ -26,25 +26,13 @@
|
|
|
- (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.topAnchor constraintEqualToAnchor:self.topAnchor constant:0],
|
|
|
[self.stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
|
|
|
[self.stackView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
|
|
|
- [self.stackView.bottomAnchor constraintEqualToAnchor:self.safeAreaLayoutGuide.bottomAnchor constant:-8]
|
|
|
+ [self.stackView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:0]
|
|
|
]];
|
|
|
}
|
|
|
|
|
|
@@ -70,8 +58,10 @@
|
|
|
normalImage:(NSString *)normalImage
|
|
|
selectedImage:(NSString *)selectedImage {
|
|
|
|
|
|
- QMUIButton *button = [QMUIButton buttonWithType:UIButtonTypeCustom];
|
|
|
- button.imagePosition = QMUIButtonImagePositionTop;
|
|
|
+ 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;
|
|
|
|
|
|
// 设置图片
|
|
|
@@ -85,27 +75,50 @@
|
|
|
[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];
|
|
|
|
|
|
+ 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: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 (self.tabBarButtons.count == 1) {
|
|
|
+ if (isFirst) {
|
|
|
[self setSelectedIndex:0 animated:NO];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)tabBarButtonClicked:(UIButton *)sender {
|
|
|
+ if (sender.isSelected) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
NSInteger index = sender.tag;
|
|
|
[self setSelectedIndex:index animated:YES];
|
|
|
|
|
|
@@ -129,18 +142,10 @@
|
|
|
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;
|
|
|
- }];
|
|
|
- }
|
|
|
- }];
|
|
|
- }
|
|
|
+ [button mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.size.mas_equalTo(button.isSelected ? 60 : 28);
|
|
|
+ make.top.mas_offset(button.isSelected ? 10 : 16);
|
|
|
+ }];
|
|
|
}
|
|
|
}
|
|
|
|