這是我正在進行的一個專案,基本上可以讓我有一個歡迎頁面。單擊“單擊此處”后,按下的按鈕應使新背景充滿新顏色(創建新場景)...我希望以后會添加更多內容。
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()
}
}
uj5u.com熱心網友回復:
如果你格式化你的代碼(選擇和使用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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/481926.html
上一篇:如何使用單個變數設定復選框的值
下一篇:在R中繪制輪子(餅圖)圖
