英文:
How to access VStack from within its onAppear closure?
问题
如何在其自己的onAppear
闭包中访问VStack
?我尝试将堆栈作为ContentView
的实例属性,但了解到VStack
不是一种类型,而是一种类型构造器。我的选择有哪些,最佳实践是什么?
struct ContentView: View {
var body: some View {
VStack {
}
.background(.red)
.onAppear {
// 将VStack的背景颜色更改为蓝色
}
}
}
英文:
How do I access the VStack
from within its own onAppear
closure? I tried making the stack an instance property of ContentView
but learned that VStack
is not a type, but a type constructor. What are my options and what is best practice?
struct ContentView: View {
var body: some View {
VStack {
}
.background(.red)
.onAppear {
// change VStack background color to blue
}
}
}
答案1
得分: 1
你无法访问VStack
的内部,但可以创建变量,VStack
和onAppear
都可以访问。
struct ContentView: View {
@State private var vStackBackground: Color = .red
var body: some View {
VStack {
Text("测试")
}
.background(vStackBackground)
.onAppear {
self.vStackBackground = .blue
}
}
}
英文:
You can't access the VStack
internals but you can create a variables that both the VStack
and the onAppear
can access.
struct ContentView: View {
@State private var vStackBackground: Color = .red
var body: some View {
VStack {
Text("Test")
}
.background(vStackBackground)
.onAppear {
self.vStackBackground = .blue
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论