首页 > 解决方案 > 如何用黑色填充灰色空间?SwiftUI

问题描述

如何用黑色绘制一个完全灰色的空间?看图片 这是我的代码:图片

.onTapGesture {
            self.showModal = true
        }.sheet(isPresented: self.$showModal, onDismiss: {
//            addEntry()
//            requestReviewIfNeeded()
            self.showModal = false
        }, content: {
            
            BiographyDetail(biography: self.biography)
                
            
            Button(action: {
                showModal = false
            }) {
                Text("CLOSE")
                    .foregroundColor(.gray)
                    .background(Color.black).ignoresSafeArea()
            }
  
        })

感谢您的帮助!

标签: iosswiftxcodeswiftui

解决方案


这是一种可能的方法 - 使用颜色作为背景并将特定内容放置在此颜色的叠加层中。

使用 Xcode 13 / iOS 15 测试

.sheet(isPresented: $showModal) {
    Color.black
        .ignoresSafeArea()   // << consumes all space
        .overlay(            // << content is overlaid 
            VStack(spacing: 0) {
                BiographyDetail(biography: self.biography)


                Button(action: {
                    showModal = false
                }) {
                    Text("CLOSE")
                        .foregroundColor(.gray)
                        .background(Color.black).ignoresSafeArea()
                }
            })
}

推荐阅读