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

SwiftUI底部Overlay实现顶部圆角+覆盖安全区问题求助

解决方案

问题出在你把裁剪和背景只加在了HStack上,导致底部安全区的部分没有被背景覆盖。调整层级,把背景和裁剪应用到整个safeAreaInset里的VStack上就能解决:

NavigationStack {
    ScrollView {
        LazyVGrid(columns: columns, pinnedViews: .sectionHeaders) {
        }
        .padding(.horizontal)
        Text("Dunno").font(.title).padding(.vertical)
        LazyVGrid(columns: columns, pinnedViews: .sectionHeaders) {
        }
        .padding(.horizontal)
    }
    .navigationTitle("App")
    .safeAreaInset(edge: .bottom, spacing: 0) {
        VStack {
            Rectangle()
                .frame(width: 70, height: 3)
                .foregroundColor(Color.white)
                .padding()
            HStack {
                TextField("Dunno", text: $searchText)
                Button("Test") {} // 补全Button闭包避免编译错误
                LazyVGrid(columns: columns, pinnedViews: .sectionHeaders) {
                }
                Spacer()
            }
            .frame(maxWidth: .infinity, maxHeight: sheetHeight)
            .padding()
        }
        .background(.gray) // 将背景移到VStack外层
        .clipShape(
            .rect(
                topLeadingRadius: 10,
                bottomLeadingRadius: 0,
                bottomTrailingRadius: 0,
                topTrailingRadius: 10
            )
        ) // 将裁剪也移到VStack外层
    }
}

修改后,整个底部嵌入视图会填满安全区,同时只有顶部会被裁剪出圆角,刚好满足你的需求。另外补全了Button的空闭包,避免编译报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 17:21:05