swift - Swifui 背景颜色没有改变
问题描述
我有一个非常简单的屏幕我只需要更改屏幕的背景颜色
我的代码
struct HomeView: View {
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
let screenSize = UIScreen.main.bounds.size
var body: some View {
VStack{
Text("Hey! Welcome")
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color.black)
Text("We deliver on-demand fresh fruits directly from your nearby farms.")
.font(.body)
.fontWeight(.medium)
.foregroundColor(Color.gray).padding(1)
.multilineTextAlignment(.center)
Button(action: {
}) {
Text("Get Started").foregroundColor(Color.black).fontWeight(.medium).padding(10)
}
.frame(maxWidth: screenWidth * 0.875)
.background(Color.yellow)
.cornerRadius(12)
Button(action: {
}) {
Text("I already have an account").foregroundColor(Color.black).fontWeight(.medium).padding(10)
}
.frame(maxWidth: screenWidth * 0.875)
.background(Color.white)
.cornerRadius(12)
}
}
}
如果我Color.purple.ignoresSafeArea()
在 VStack 旁边或内部使用它会改变颜色,但我的文本等所有东西都会触底
我只需要更改我的背景颜色并使框透明,它显示为白色和紫色。
解决方案
试试这个代码,这对我有用。
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) {}
}
例子:
VStack{
GenderVW(showModal: .constant(true), selectedGender: .constant(Gender(id: 0, gender: "Male")))
}.background(BackgroundClearView())
推荐阅读
- python - 如何将kanboard与电报集成?
- tensorflow - Tensorflow - 固定卷积核
- apache-spark - 加入性能问题:数据帧与应用了 UDF 的相同数据帧
- informatica - 有人可以帮助我了解不同的 informatica 查找缓存吗?
- javascript - 如何在本机反应中推送数组中的不同变量
- asp.net-core - 如何在 Asp.net 核心中获取静态类或 IEnumerable 中的根文件夹路径?
- php - PHP Curl - 在下载之前检查来自 URLA 或 B 的响应
- c# - 可以使用公会 ID 和角色名称设置用户角色
- android - 如何解决 GooglePlayConsole 中的“问题:需要测试登录”问题
- excel - Excel - 在一个单元格中连接值