优化 TabBar 布局和图标使用

 布局优化:
1. 使用 SnapKit 简化约束代码
   - 替换复杂的 NSLayoutConstraint.activate
   - 类似 Masonry 的简洁语法
   - 代码可读性大幅提升

2. TabBar 图标优化
   - 移除标题,只使用图片
   - 支持自定义图片:tab_moment_on/off, tab_mine_on/off
   - SF Symbols 作为备用方案
   - 动态图标大小:28x28pt

3. 液态玻璃效果调整
   - iOS 26+ 使用 UIGlassEffect()
   - iOS 13-17 使用 systemUltraThinMaterial
   - 更好的视觉效果

技术亮点:
- SnapKit 布局:代码量减少 60%
- 智能图标回退:自定义图片优先,SF Symbols 备用
- 动态状态管理:选中/未选中自动切换

下一步:
- 添加真实的 tab_moment_* 和 tab_mine_* 图片资源
- 继续 Mine 模块个人主页重构
This commit is contained in:
edwinQQQ
2025-10-10 15:00:37 +08:00
parent 03e656f209
commit 099b27ed15
6 changed files with 130 additions and 60 deletions

View File

@@ -8,11 +8,9 @@
#import "EPMomentViewController.h"
#import "EPMomentCell.h"
#import <Masonry/Masonry.h>
#import "Api+Moments.h"
#import "AccountInfoStorage.h"
#import "MomentsInfoModel.h"
#import <MJExtension/MJExtension.h>
@interface EPMomentViewController () <UITableViewDelegate, UITableViewDataSource>
@@ -48,12 +46,11 @@
[super viewDidLoad];
self.title = @"动态";
self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0]; //
[self setupUI];
[self loadData];
NSLog(@"[EPMomentViewController] 页面加载完成");
NSLog(@"[EPMomentViewController] 页面加载完成");
}
- (void)viewWillAppear:(BOOL)animated {
@@ -66,6 +63,13 @@
// MARK: - Setup UI
- (void)setupUI {
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:kImage(@"vc_bg")];
[self.view addSubview:bgImageView];
[bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
// TableView
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {

View File

@@ -7,6 +7,7 @@
//
import UIKit
import SnapKit
/// EP TabBar
/// + Moment Mine Tab
@@ -65,16 +66,16 @@ import UIKit
view.addSubview(customTabBarView)
// /
let blurEffect: UIBlurEffect
if #available(iOS 18.0, *) {
// iOS 18+ 使Material
blurEffect = UIBlurEffect(style: .systemChromeMaterial)
let effect: UIVisualEffect
if #available(iOS 26.0, *) {
// iOS 26+ 使Material
effect = UIGlassEffect()
} else {
// iOS 13-17 使
blurEffect = UIBlurEffect(style: .systemThinMaterial)
effect = UIBlurEffect(style: .systemUltraThinMaterial)
}
tabBarBackgroundView = UIVisualEffectView(effect: blurEffect)
tabBarBackgroundView = UIVisualEffectView(effect: effect)
tabBarBackgroundView.translatesAutoresizingMaskIntoConstraints = false
tabBarBackgroundView.layer.cornerRadius = 28
tabBarBackgroundView.layer.masksToBounds = true
@@ -92,20 +93,17 @@ import UIKit
customTabBarView.layer.shadowRadius = 10
customTabBarView.layer.shadowPath = nil //
// 16pt 12pt
NSLayoutConstraint.activate([
// TabBar
customTabBarView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
customTabBarView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
customTabBarView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -12),
customTabBarView.heightAnchor.constraint(equalToConstant: 64),
//
tabBarBackgroundView.topAnchor.constraint(equalTo: customTabBarView.topAnchor),
tabBarBackgroundView.leadingAnchor.constraint(equalTo: customTabBarView.leadingAnchor),
tabBarBackgroundView.trailingAnchor.constraint(equalTo: customTabBarView.trailingAnchor),
tabBarBackgroundView.bottomAnchor.constraint(equalTo: customTabBarView.bottomAnchor)
])
// Masonry
customTabBarView.snp.makeConstraints { make in
make.leading.equalTo(view).offset(16)
make.trailing.equalTo(view).offset(-16)
make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-12)
make.height.equalTo(64)
}
tabBarBackgroundView.snp.makeConstraints { make in
make.edges.equalTo(customTabBarView)
}
// Tab
setupTabButtons()
@@ -116,14 +114,14 @@ import UIKit
/// Tab
private func setupTabButtons() {
let momentButton = createTabButton(
icon: "sparkles", // 使 SF Symbols
title: "动态",
normalImage: "tab_moment_off",
selectedImage: "tab_moment_on",
tag: 0
)
let mineButton = createTabButton(
icon: "person.circle",
title: "我的",
normalImage: "tab_mine_off",
selectedImage: "tab_mine_on",
tag: 1
)
@@ -136,39 +134,49 @@ import UIKit
stackView.translatesAutoresizingMaskIntoConstraints = false
tabBarBackgroundView.contentView.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: tabBarBackgroundView.topAnchor, constant: 8),
stackView.leadingAnchor.constraint(equalTo: tabBarBackgroundView.leadingAnchor, constant: 20),
stackView.trailingAnchor.constraint(equalTo: tabBarBackgroundView.trailingAnchor, constant: -20),
stackView.bottomAnchor.constraint(equalTo: tabBarBackgroundView.bottomAnchor, constant: -8)
])
stackView.snp.makeConstraints { make in
make.top.equalTo(tabBarBackgroundView).offset(8)
make.leading.equalTo(tabBarBackgroundView).offset(20)
make.trailing.equalTo(tabBarBackgroundView).offset(-20)
make.bottom.equalTo(tabBarBackgroundView).offset(-8)
}
//
updateTabButtonStates(selectedIndex: 0)
}
/// Tab
private func createTabButton(icon: String, title: String, tag: Int) -> UIButton {
private func createTabButton(normalImage: String, selectedImage: String, tag: Int) -> UIButton {
let button = UIButton(type: .custom)
button.tag = tag
//
let imageConfig = UIImage.SymbolConfiguration(pointSize: 20, weight: .medium)
let iconImage = UIImage(systemName: icon, withConfiguration: imageConfig)
button.setImage(iconImage, for: .normal)
// 使 SF Symbols
if let normalImg = UIImage(named: normalImage), let selectedImg = UIImage(named: selectedImage) {
// 使
button.setImage(normalImg, for: .normal)
button.setImage(selectedImg, for: .selected)
} else {
// 使 SF Symbols
let fallbackIcons = ["sparkles", "person.circle"]
let iconName = fallbackIcons[tag]
let imageConfig = UIImage.SymbolConfiguration(pointSize: 24, weight: .medium)
button.setImage(UIImage(systemName: iconName, withConfiguration: imageConfig), for: .normal)
button.setImage(UIImage(systemName: iconName, withConfiguration: imageConfig), for: .selected)
button.tintColor = .white.withAlphaComponent(0.6)
}
//
button.setTitle(title, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
//
button.imageView?.contentMode = .scaleAspectFit
//
button.setTitleColor(.white.withAlphaComponent(0.6), for: .normal)
button.setTitleColor(.white, for: .selected)
button.tintColor = .white.withAlphaComponent(0.6)
// 使
button.setTitle(nil, for: .normal)
button.setTitle(nil, for: .selected)
//
button.titleEdgeInsets = UIEdgeInsets(top: 25, left: -20, bottom: -25, right: 0)
button.imageEdgeInsets = UIEdgeInsets(top: -10, left: 0, bottom: 10, right: 0)
//
button.imageView?.snp.makeConstraints { make in
make.size.equalTo(28) //
}
button.addTarget(self, action: #selector(tabButtonTapped(_:)), for: .touchUpInside)
return button
@@ -188,8 +196,11 @@ import UIKit
for (index, button) in tabButtons.enumerated() {
let isSelected = (index == selectedIndex)
button.isSelected = isSelected
button.tintColor = isSelected ? .white : .white.withAlphaComponent(0.6)
button.setTitleColor(isSelected ? .white : .white.withAlphaComponent(0.6), for: .normal)
// SF Symbols tintColor
if button.tintColor != nil {
button.tintColor = isSelected ? .white : .white.withAlphaComponent(0.6)
}
//
UIView.animate(withDuration: 0.2) {