ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

swiftui-ui-patterns - list

swiftui-ui-patterns - list 列表与分区List and Section意图对于信息流式内容和设置风格的行使用List此时内置的行复用、选择和可访问性很重要。核心模式对带有重复行的长垂直滚动内容优先使用List。使用Section标题对相关行分组。需要滚动到顶部或跳转到指定 id 时配合ScrollViewReader。对现代信息流布局使用.listStyle(.plain)。对分区分组有帮助的多分区发现/搜索页使用.listStyle(.grouped)。需要主题化表面时应用.scrollContentBackground(.hidden) 自定义背景。使用.listRowInsets(...)和.listRowSeparator(.hidden)调整行间距和分隔线。使用.environment(\\.defaultMinListRowHeight, ...)控制紧凑列表布局。示例带滚动到顶部的信息流列表MainActorstructTimelineListView:View{Environment(\.selectedTabScrollToTop)privatevarselectedTabScrollToTopStateprivatevarscrollToId: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)scrollToIdnil}}.onChange(of:selectedTabScrollToTop){_,newValueinifnewValue0{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行内使用重型自定义布局改用ScrollViewLazyVStack。小心混用List和嵌套的ScrollView可能导致手势冲突。
返回列表