SwiftUI中ScrollView异常求助:标签栏上方灰色区域无法消除
SwiftUI ScrollView 底部灰色区域异常问题
我正在巩固SwiftUI相关知识并开发一个任务页面,遇到ScrollView的异常问题:标签栏上方存在一块无法消除的灰色区域(见滚动至顶部截图),当滚动至应用底部时该区域会消失。
注:我使用了自定义TabBar,且ContentView中所有视图都嵌套在ZStack内。
struct DailyTaskView: View { var tasks: [Task] var body: some View { ZStack { Color.gBlack.ignoresSafeArea(.all) ScrollView(.vertical, showsIndicators: false) { VStack { ForEach(tasks) { task in TaskItemView(task: task) .padding(.vertical, 10) } } } } } }
struct TaskItemView: View { var task: Task var body: some View { HStack(alignment: .center, spacing: 40) { Circle() .frame(width: 10, height: 10) .foregroundColor(.white) .shadow(color: .white.opacity(0.1), radius: 3) VStack(alignment: .leading, spacing: 8) { HStack(spacing: 8) { Text(task.title) .foregroundStyle(Color.white) .font(.title) Spacer() Label("11:22 PM", systemImage: "clock") .foregroundStyle(Color.white) } Text(task.description) .foregroundStyle(Color.white) .font(.callout) } // .frame(maxWidth: .infinity) .padding(15) .background(task.tint, in: .rect(topLeadingRadius: 15, bottomLeadingRadius: 15)) // .clipShape(.rect(cornerRadius: 20)) .strikethrough(task.isCompleted, pattern: .solid, color: .white) } .padding(.leading) } }
struct ContentView: View { @State private var tabSelection: Int = 0 @State private var tasks: [Task] = sampleTask.sorted(by: {$1.date > $0.date}) var body: some View { ZStack { Color.gBlack.ignoresSafeArea(.all) VStack() { DateHeaderView() .frame(maxWidth: .infinity) .background(Color(Color.gBlack)) TabView(selection: $tabSelection) { DailyTaskView(tasks: tasks) .tag(0) Text("Tab 2") .foregroundStyle(Color.gBlack) .font(.largeTitle) .fontWeight(.black) .tag(1) } Spacer() TabBarView(tabSelection: $tabSelection) } .ignoresSafeArea(edges: .bottom) } } }
滚动至顶部时的效果:
滚动至底部时的效果:
已尝试过以下方法但未解决:
- 改用List组件,确认是否为ScrollView特有问题
- 调整.frame()属性,无效果
- 检查是否存在额外的padding影响
内容的提问来源于stack exchange,提问作者Joseph
相关产品推荐
相关产品推荐

