☰
swiftui-ui-patterns - list
2026/10/7 16:23:27 网站建设 项目流程

列表与分区(List and Section)

意图

对于信息流式内容和设置风格的行,使用List,此时内置的行复用、选择和可访问性很重要。

核心模式

  • 对带有重复行的长垂直滚动内容,优先使用List。
  • 使用Section标题对相关行分组。
  • 需要滚动到顶部或跳转到指定 id 时,配合ScrollViewReader。
  • 对现代信息流布局使用.listStyle(.plain)。
  • 对分区分组有帮助的多分区发现/搜索页,使用.listStyle(.grouped)。
  • 需要主题化表面时,应用.scrollContentBackground(.hidden)+ 自定义背景。
  • 使用.listRowInsets(...)和.listRowSeparator(.hidden)调整行间距和分隔线。
  • 使用.environment(\\.defaultMinListRowHeight, ...)控制紧凑列表布局。

示例:带滚动到顶部的信息流列表

@MainActorstructTimelineListView:View{@Environment(\.selectedTabScrollToTop)privatevarselectedTabScrollToTop@StateprivatevarscrollToId:String?varbody:someView{ScrollViewReader{proxyinList{ForEach(items){iteminTimelineRow(item:item).id(item.id).listRowInsets(.init(top:12,leading:16,bottom:6,trailing:16)).listRowSeparator(.hidden)}}.listStyle(.plain).environment(\\.defaultMinListRowHeight,1).onChange(of:scrollToId){_,newValueinifletnewValue{proxy.scrollTo(newValue,anchor:.top)scrollToId=nil}}.onChange(of:selectedTabScrollToTop){_,newValueinifnewValue==0{withAnimation{proxy.scrollTo(ScrollToView.Constants.scrollToTop,anchor:.top)}}}}}}

示例:设置风格列表

@MainActorstructSettingsView:View{varbody:someView{List{Section("General"){NavigationLink("Display"){DisplaySettingsView()}NavigationLink("Haptics"){HapticsSettingsView()}}Section("Account"){Button("Sign Out",role:.destructive){}}}.listStyle(.insetGrouped)}}

应保留的设计选择

  • 对动态信息流、设置以及任何行语义有帮助的 UI 使用List。
  • 为行使用稳定的 ID,以保持动画和滚动定位可靠。
  • 对应当整行可点击的行,优先使用.contentShape(Rectangle())。
  • 当数据源支持时,对下拉刷新信息流使用.refreshable。

陷阱

  • 避免在List行内使用重型自定义布局;改用ScrollView+LazyVStack。
  • 小心混用List和嵌套的ScrollView;可能导致手势冲突。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询