Swift UI - 我正在尝试更改背景。它在函数 buttonPressed() 下给我一个错误
Swift UI - I am trying to change the background. It is giving me an error under the function buttonPressed()
这是我正在做的一个项目,基本上可以让我有一个欢迎页面。单击 'click here' 后,按下的按钮应该会使新背景充满新颜色(创建新场景)...我希望稍后添加更多内容。
import SwiftUI
struct ContentView: View {
// defined global variables in a function
@State var backgroundColor: Color = Color.red
var body: some View {
ZStack {
// background called
backgroundColor
.edgesIgnoringSafeArea(.all)
// content
contentLayer
}
}
var contentLayer: some View {
// content
VStack {
Text("Welcome to newsapptest!")
.font(.largeTitle)
Button(action: {
buttonPressed()
}, label: {
Text("Click here to continue to the app!")
.font(.headline)
.foregroundColor(.white)
.padding()
.background(Color.black)
.cornerRadius(10)
})
}
}
}
func buttonPressed() {
backgroundColor = .blue
backgroundColor = .blue 给我一个问题,说它不在我的范围内。我该如何解决这个问题?
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
如果您格式化您的代码(select 并使用 ctrl-i
),您会发现您的 buttonPressed
函数实际上 在 之外你的 ContentView
结构。将它移到里面,它会正确编译:
struct ContentView: View {
// defined global variables in a function
@State var backgroundColor: Color = Color.red
var body: some View {
ZStack {
// background called
backgroundColor
.edgesIgnoringSafeArea(.all)
// content
contentLayer
}
}
var contentLayer: some View {
// content
VStack {
Text("Welcome to newsapptest!")
.font(.largeTitle)
Button(action: {
buttonPressed()
}, label: {
Text("Click here to continue to the app!")
.font(.headline)
.foregroundColor(.white)
.padding()
.background(Color.black)
.cornerRadius(10)
})
}
}
func buttonPressed() {
backgroundColor = .blue
}
}
这是我正在做的一个项目,基本上可以让我有一个欢迎页面。单击 'click here' 后,按下的按钮应该会使新背景充满新颜色(创建新场景)...我希望稍后添加更多内容。
import SwiftUI
struct ContentView: View {
// defined global variables in a function
@State var backgroundColor: Color = Color.red
var body: some View {
ZStack {
// background called
backgroundColor
.edgesIgnoringSafeArea(.all)
// content
contentLayer
}
}
var contentLayer: some View {
// content
VStack {
Text("Welcome to newsapptest!")
.font(.largeTitle)
Button(action: {
buttonPressed()
}, label: {
Text("Click here to continue to the app!")
.font(.headline)
.foregroundColor(.white)
.padding()
.background(Color.black)
.cornerRadius(10)
})
}
}
}
func buttonPressed() {
backgroundColor = .blue
backgroundColor = .blue 给我一个问题,说它不在我的范围内。我该如何解决这个问题?
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
如果您格式化您的代码(select 并使用 ctrl-i
),您会发现您的 buttonPressed
函数实际上 在 之外你的 ContentView
结构。将它移到里面,它会正确编译:
struct ContentView: View {
// defined global variables in a function
@State var backgroundColor: Color = Color.red
var body: some View {
ZStack {
// background called
backgroundColor
.edgesIgnoringSafeArea(.all)
// content
contentLayer
}
}
var contentLayer: some View {
// content
VStack {
Text("Welcome to newsapptest!")
.font(.largeTitle)
Button(action: {
buttonPressed()
}, label: {
Text("Click here to continue to the app!")
.font(.headline)
.foregroundColor(.white)
.padding()
.background(Color.black)
.cornerRadius(10)
})
}
}
func buttonPressed() {
backgroundColor = .blue
}
}