如何在工作表视图上制作不透明的背景颜色? [英] How can I make a background color with opacity on a Sheet view?

查看:25
本文介绍了如何在工作表视图上制作不透明的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 SwiftUI 开发应用程序.

I'm currently developing an application using SwiftUI.

我正在寻找某种方法在工作表视图上制作不透明的背景颜色.

I'm looking for some way to make a background color with opacity on a Sheet view.

有没有办法做到这一点?

is there any way to do that?

我尝试使用下面的代码来做到这一点,我可以使用 opacity 属性更改颜色,但是我在工作表视图下看不到文本(工作表)...

I've tried to do that with a code below, I can change the color with opacity property, but I can't see a text(Sheet) under the sheet View...

import SwiftUI

struct ContentView: View {
    
    @State var isSheet = false
    
    var body: some View {
       
        Button(action: {self.isSheet.toggle()}) {
            Text("Sheet")
        }.sheet(isPresented: $isSheet){
            Color.yellow.opacity(0.5)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}  


Xcode:11.7 版


Xcode: Version 11.7

斯威夫特:斯威夫特 5

Swift: Swift 5

推荐答案

您无法使用标准的工作表官方 API(因为默认情况下每个托管控制器视图都是不透明的),因此您可以创建自定义工作表视图(具有任何功能)您需要)或使用运行时解决方法来查找该视图并将其背景设置为清除.如下图(仅用于演示)

You cannot do it with standard sheet official API (because every hosting controller view by default is opaque), so you can either create custom sheet view (with any features you needed) or use run-time workaround to find that view and set its background to clear. Like below (only for demo)

struct DemoView: View {

    @State var isSheet = false

    var body: some View {

        Button(action: {self.isSheet.toggle()}) {
            Text("Sheet")
        }.sheet(isPresented: $isSheet){
            Color.yellow.opacity(0.5)
                .background(BackgroundClearView())
        }
    }
}

struct BackgroundClearView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let view = UIView()
        DispatchQueue.main.async {
            view.superview?.superview?.backgroundColor = .clear
        }
        return view
    }

    func updateUIView(_ uiView: UIView, context: Context) {}
}

这篇关于如何在工作表视图上制作不透明的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆