当前位置: 首页 > news >正文

ios开发: 自定义tabview,页面可拖动切换

一,代码:


import SwiftUIstruct ContentView3: View {@State private var selectedTab = 0var body: some View {ZStack(alignment: .bottom) {// 使用 Page 样式实现左右滑动动画TabView(selection: $selectedTab) {HomeView2().tag(0)ListView2().tag(1)AboutView().tag(2)}// 核心设置:开启分页滑动模式// indexDisplayMode: .never 隐藏底部的原生小圆点.tabViewStyle(.page(indexDisplayMode: .never)).animation(.easeInOut, value: selectedTab) // 点击切换时的平滑动画// 自定义底部栏(因为 .page 模式下原生 TabBar 会消失)customTabBar}.ignoresSafeArea(.keyboard) // 避免键盘弹出影响布局}// 抽离出的自定义底部栏var customTabBar: some View {HStack {TabBarItem(title: "首页", icon: "house.fill", index: 0, selection: $selectedTab)Spacer()TabBarItem(title: "列表", icon: "list.bullet", index: 1, selection: $selectedTab)Spacer()TabBarItem(title: "关于", icon: "person.circle", index: 2, selection: $selectedTab)}.padding(.horizontal, 40).frame(height: 100) // 这里调高高度.background(Color(.systemGray6)).shadow(color: .black.opacity(0.1), radius: 10, y: -5)}
}// 按钮组件
struct TabBarItem: View {let title: Stringlet icon: Stringlet index: Int@Binding var selection: Intvar body: some View {Button {withAnimation { selection = index }} label: {VStack(spacing: 4) {Image(systemName: icon).font(.system(size: 22))Text(title).font(.caption2)}.foregroundColor(selection == index ? .blue : .gray)}}
}// --- 子页面定义 ---struct HomeView2: View {var body: some View {VStack {Text("这是首页").font(.headline)Image(systemName: "globe") // 使用系统图标.resizable().aspectRatio(contentMode: .fit).frame(width: 200, height: 200).foregroundColor(.blue)}}
}struct ListView2: View {var body: some View {VStack {Text("内容列表").font(.headline)Image(systemName: "doc.text.magnifyingglass").resizable().aspectRatio(contentMode: .fit).frame(width: 200, height: 200).foregroundColor(.green)}}
}struct AboutView: View {var body: some View {VStack {Text("关于我们").font(.headline)Image(systemName: "info.circle.fill").resizable().aspectRatio(contentMode: .fit).frame(width: 200, height: 200).foregroundColor(.orange)}}
}

 

二,测试效果:

image

 

http://www.jsqmd.com/news/623054/

相关文章:

  • 如何轻松实现网盘免客户端高速下载?这款免费助手给你完美解决方案
  • 别再手动改时间了!Ubuntu 22.04 用 timedatectl 一键切换时区到 Asia/Shanghai 的完整指南
  • 2026年药物研发用低温差示扫描量热仪排名,上海皆准仪器上榜 - myqiye
  • NeverSink-Filter的碎片化、通货、圣甲虫等20+分层类型详解
  • firecracker-containerd 安全机制全解析:从文件系统隔离到网络防护
  • 避开汇川机器人码垛的坑:从‘五点法’标定到夹爪干涉避让的完整指南
  • GHelper:华硕笔记本硬件控制的三大场景革新 - 从性能优化到专业调校
  • php5.5: 编译时报错
  • Stable-Diffusion-v1-5-archive安全与合规使用指南:内容过滤与版权风险规避
  • 说说全国低温差示扫描量热仪服务厂商,哪家性价比高? - mypinpai
  • 终极指南:在Windows上使用Switch Joy-Con控制器的完整解决方案
  • 别再写死UI了!用QML的ListView+ListModel动态渲染数据列表(附完整代码)
  • BRPickerView:iOS开发者的终极选择器组件解决方案
  • 终极解决方案:让老旧Mac焕发新生的完整指南
  • AlphaFold批量处理实战:从单序列到高通量预测的效率革命
  • 终极指南:5分钟掌握Blender与ZBrush无缝桥接的GoB插件
  • 西湖区舞蹈培训深度测评:2026年至今,这五家工作室为何脱颖而出? - 2026年企业推荐榜
  • 小白也能懂!通义千问多模态重排序服务Web UI部署指南
  • CANoe CAPL实战:我是如何从零搭建UDS Bootloader自动化测试脚本的(附避坑点)
  • Vue 项目实战:基于 vxe-table 的动态高度虚拟滚动表格性能调优与避坑指南
  • VMware ESXi 9.0.2.0 macOS Unlocker OEM BIOS 2.7 集成 Realtek 网卡驱动定制版
  • 保姆级教程:用Python脚本下载ScanNet数据集(附子集下载与.sens文件提取)
  • Blazor快速接入失败率下降76%的关键配置,微软MVP验证的4项必检清单
  • 3步解锁B站4K视频下载:告别网络限制,建立个人高清资源库
  • VCF 5.2.2 非生产环境优化:vSAN ESA HCL 检查绕过实操教程
  • CDN的应用场景:静态资源加速、视频点播加速的优势
  • 如何用Zotero Style插件实现智能文献管理:从阅读进度到标签可视化的完整指南
  • 如何快速部署YaeAchievement:原神成就数据自动化导出终极指南
  • UniApp安卓端后台保活插件实战:告别息屏被杀,让你的App持续运行
  • NaViL-9B开源大模型落地:金融票据识别+风险点标注自动化案例