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

如何实现类似iPhone设置的表单滚动视觉效果及问题解决

需求

想要实现如下视觉效果:一个类似iPhone设置应用的表单,顶部带有圆角视觉元素,且两者可一起滚动、背景颜色一致。
目标效果:顶部圆角元素与表单同步滚动,背景颜色统一

当前代码
NavigationStack {
    HStack {
        Image("JoinUs") // Demo purpose
            .resizable()
            .frame(maxWidth: 360)
            .aspectRatio(contentMode: .fit)
    }
    .background(.green)
    .mask { RoundedRectangle(cornerRadius: 25) }
    .padding()
    
    Form {
        Section {
            Text("Voice")
            Text("Speed")
            Text("Pitch")
            Text("Voice")
        } header: {
            Text("Sound")
        }
        
        Section {
            Text("Text Size")
        } header: {
            Text("System")
        }
        
        Section {
            Text("Voice")
            Text("Speed")
            Text("Pitch")
            Text("Voice")
        } header: {
            Text("Sound")
        }
        
        Section {
            Text("Voice")
            Text("Speed")
            Text("Pitch")
            Text("Voice")
        } header: {
            Text("Sound")
        }
    }
    .navigationTitle("Settings")
}
当前实现效果

当前效果:顶部HStack背景为白色,且不随表单滚动

遇到的问题
  • 顶部HStack视图背景为白色,无法显示与表单一致的灰色背景
  • 顶部HStack视图无法随表单滚动;尝试将其放入Form后可一起滚动,但尺寸变小且白色背景问题仍存在
    放入Form后的问题:顶部元素尺寸缩小,背景仍为白色
解决方案

要实现目标效果,需将顶部圆角元素整合到Form的滚动容器中,同时统一全局背景色,具体调整代码如下:

NavigationStack {
    Form {
        // 顶部圆角元素所在的Section
        Section {
            HStack {
                Image("JoinUs") // Demo purpose
                    .resizable()
                    .frame(maxWidth: .infinity)
                    .aspectRatio(contentMode: .fit)
            }
            .background(.green)
            .mask(RoundedRectangle(cornerRadius: 25))
            .padding(.vertical)
        }
        .listRowBackground(Color.clear)
        .listSectionSeparator(.hidden)
        
        // 原有表单内容
        Section {
            Text("Voice")
            Text("Speed")
            Text("Pitch")
            Text("Voice")
        } header: {
            Text("Sound")
        }
        
        Section {
            Text("Text Size")
        } header: {
            Text("System")
        }
        
        Section {
            Text("Voice")
            Text("Speed")
            Text("Pitch")
            Text("Voice")
        } header: {
            Text("Sound")
        }
        
        Section {
            Text("Voice")
            Text("Speed")
            Text("Pitch")
            Text("Voice")
        } header: {
            Text("Sound")
        }
    }
    .navigationTitle("Settings")
    .scrollContentBackground(.hidden)
    .background(.groupedBackground)
}

关键调整说明

  1. 统一背景:通过scrollContentBackground(.hidden)隐藏Form默认滚动背景,再设置.background(.groupedBackground)让全局背景与系统设置表单一致
  2. 整合滚动:将顶部元素放入Form的第一个Section,实现与表单同步滚动;用listRowBackground(Color.clear)去掉Section默认白色背景,避免视觉割裂
  3. 修复尺寸:给HStack设置maxWidth: .infinity使其充满Section宽度,解决放入Form后尺寸缩小的问题
  4. 优化视觉:用listSectionSeparator(.hidden)隐藏顶部Section的分隔线,让整体布局更连贯

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 21:46:35