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

如何在VisionOS中实现窗口透明且保留窗口原有内容?

VisionOS 透明窗口实现方案(保留内容)

直接给视图加.background(.clear)无效的原因是:VisionOS 默认的WindowGroup容器和TabView都自带不透明背景层,必须从窗口配置到视图层级逐一处理:

1. 配置窗口支持透明

给WindowGroup添加透明度允许、透明背景材质和纯窗口样式,移除容器级的不透明背景:

WindowGroup {
    TabView {
        ViewWithTransparentBackground()
            .tabItem {
                Label("透明页面", systemImage: "circle.fill")
            }
        ViewWithDefaultBackground()
            .tabItem {
                Label("默认页面", systemImage: "square.fill")
            }
    }
    // 移除TabView自身的默认背景
    .background(.clear)
    // 设置窗口样式为无装饰的纯样式
    .windowStyle(.plain)
}
// 核心:允许窗口渲染透明区域
.allowsTransparency(true)
// 移除系统默认的窗口背景材质
.windowBackgroundMaterial(.clear)

2. 实现透明内容视图

确保目标视图本身背景透明,同时调整内容颜色保证可读性:

struct ViewWithTransparentBackground: View {
    var body: some View {
        VStack(spacing: 20) {
            Text("透明窗口内的可见内容")
                .font(.title)
                .foregroundColor(.white) // 适配空间环境的高对比度颜色
                .padding()
                .background(.black.opacity(0.4)) // 可选:给内容加半透背景提升辨识度
            Image(systemName: "globe")
                .font(.system(size: 60))
                .foregroundColor(.blue)
        }
        .background(.clear) // 明确视图背景透明
    }
}

// 保留原有默认背景视图
struct ViewWithDefaultBackground: View {
    var body: some View {
        Text("默认背景页面")
            .font(.title)
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(.gray)
    }
}

关键细节

  • 必须同时启用allowsTransparency(true)和windowBackgroundMaterial(.clear),二者缺一不可——前者允许窗口渲染透明区域,后者移除系统默认的窗口背景材质
  • 如果TabView仍有残留背景,可额外添加.tabViewStyle(.automatic)或.tabViewStyle(.page),配合.background(.clear)即可完全移除
  • 内容颜色建议选择与空间环境对比强烈的色调(如白色、亮蓝色),避免透明背景下内容模糊

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 21:23:20