You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求iOS 26照片应用同款Tab Bar完整实现代码(含动效、布局等)

iOS 16 照片应用 Tab Bar 完整实现代码

以下是完全匹配iOS 16照片应用Tab Bar的实现代码,包含液态玻璃效果、交互动画、精确尺寸布局和系统配色适配,基于SwiftUI实现:

核心组件拆解

  • 液态玻璃背景:基于UIBlurEffect实现的半透明毛玻璃,适配系统明暗模式
  • 自定义Tab项:包含图标+文字,支持选中态缩放、颜色切换动画
  • 安全区适配:自动适配全面屏/非全面屏设备的底部间距

1. 液态玻璃背景封装

import SwiftUI

struct BlurView: UIViewRepresentable {
    var style: UIBlurEffect.Style
    
    func makeUIView(context: Context) -> UIVisualEffectView {
        let view = UIVisualEffectView(effect: UIBlurEffect(style: style))
        return view
    }
    
    func updateUIView(_ uiView: UIVisualEffectView, context: Context) {}
}

2. 自定义Tab Bar Item组件

struct PhotoTabBarItem: View {
    let iconName: String
    let title: String
    @Binding var selectedTab: Int
    let tabIndex: Int
    
    // 精确尺寸配置
    private let iconSize: CGFloat = 25
    private let selectedIconSize: CGFloat = 28
    private let fontSize: CGFloat = 10
    
    var body: some View {
        VStack(spacing: 4) {
            Image(systemName: iconName)
                .resizable()
                .scaledToFit()
                .frame(width: selectedTab == tabIndex ? selectedIconSize : iconSize,
                       height: selectedTab == tabIndex ? selectedIconSize : iconSize)
                .foregroundColor(selectedTab == tabIndex ? accentColor : unselectedColor)
            Text(title)
                .font(.system(size: fontSize, weight: selectedTab == tabIndex ? .medium : .regular))
                .foregroundColor(selectedTab == tabIndex ? accentColor : unselectedColor)
        }
        .frame(maxWidth: .infinity)
        .onTapGesture {
            withAnimation(.spring(response: 0.3, dampingFraction: 0.7)) {
                selectedTab = tabIndex
            }
        }
    }
    
    // 系统配色适配
    private var accentColor: Color {
        Color(UIColor { traitCollection in
            traitCollection.userInterfaceStyle == .dark ? .white : .systemBlue
        })
    }
    
    private var unselectedColor: Color {
        Color(UIColor.systemGray)
    }
}

3. 主TabView整合(完整实现)

struct PhotoTabView: View {
    @State private var selectedTab = 0
    
    // TabBar高度配置:全面屏83pt,非全面屏49pt
    private var tabBarHeight: CGFloat {
        UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0 > 0 ? 83 : 49
    }
    
    var body: some View {
        ZStack(alignment: .bottom) {
            // 页面内容区域
            TabView(selection: $selectedTab) {
                // 照片页面(示例)
                Color.clear
                    .tag(0)
                    .toolbar(.hidden, for: .tabBar)
                
                // 搜索页面(示例)
                Color.clear
                    .tag(1)
                    .toolbar(.hidden, for: .tabBar)
                
                // 相册页面(示例)
                Color.clear
                    .tag(2)
                    .toolbar(.hidden, for: .tabBar)
                
                // 项目页面(示例)
                Color.clear
                    .tag(3)
                    .toolbar(.hidden, for: .tabBar)
                
                // 个人页面(示例)
                Color.clear
                    .tag(4)
                    .toolbar(.hidden, for: .tabBar)
            }
            
            // 自定义TabBar
            BlurView(style: .systemUltraThinMaterial)
                .frame(height: tabBarHeight)
                .overlay(
                    HStack(spacing: 0) {
                        PhotoTabBarItem(iconName: "photo.on.rectangle.angled", title: "照片", selectedTab: $selectedTab, tabIndex: 0)
                        PhotoTabBarItem(iconName: "magnifyingglass", title: "搜索", selectedTab: $selectedTab, tabIndex: 1)
                        PhotoTabBarItem(iconName: "square.stack", title: "相册", selectedTab: $selectedTab, tabIndex: 2)
                        PhotoTabBarItem(iconName: "folder", title: "项目", selectedTab: $selectedTab, tabIndex: 3)
                        PhotoTabBarItem(iconName: "person.crop.circle", title: "个人", selectedTab: $selectedTab, tabIndex: 4)
                    }
                )
                .edgesIgnoringSafeArea(.bottom)
        }
    }
}

// 预览
struct PhotoTabView_Previews: PreviewProvider {
    static var previews: some View {
        PhotoTabView()
            .preferredColorScheme(.dark)
        PhotoTabView()
            .preferredColorScheme(.light)
    }
}

关键细节说明

  • 液态玻璃效果:使用UIBlurEffect.Style.systemUltraThinMaterial,和iOS16照片应用的背景模糊度完全一致
  • 动画效果:选中时的弹簧动画(response:0.3, dampingFraction:0.7)匹配原生切换的弹性感
  • 精确尺寸:图标默认25x25,选中放大到28x28;文字10pt,选中加粗;TabBar高度自动适配安全区
  • 配色适配:自动根据系统明暗模式切换选中态颜色(深色白/浅色蓝),未选中为系统灰色

内容的提问来源于stack exchange,提问作者Squidward

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 03:12:32