我有一個非常簡單的螢屏,我只需要更改螢屏的背景顏色
我的代碼
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 旁邊或內部使用它會改變顏色但我的文字等所有東西都會變低

我只需要更改我的背景顏色并使盒子透明,它顯示白色和紫色。
uj5u.com熱心網友回復:
試試這個代碼,這對我有用。
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())
uj5u.com熱心網友回復:
嘗試像這樣簡單的事情,使用 aZStack并調整您想要的顏色:
ZStack {
Color.purple.ignoresSafeArea() // <--- here the background color
VStack {
Text("Hey! Welcome")
...
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/354974.html
