如何讓點擊按鈕時出現圖片和文字?
VStack {
Image(systemName: "house.fill").resizable().aspectRatio(contentMode: .fill) .frame(width: 75, height: 75)
Text("TEST")
Spacer().frame(height: 100)
Button(action: { }) {
Text("TEST").foregroundColor(Color.white).padding()
}.background(Color.black) .cornerRadius(10)
}
我是 swift 的新手,感謝您的幫助
uj5u.com熱心網友回復:
您可以使用if子句根據@State變數有條件地顯示元素。
我正在使用額外的frame修飾符VStack,您最初不必確保在@State變數更改和堆疊內的元素出現/消失時沒有任何位置改變。
struct ContentView : View {
@State private var showImage = false
var body: some View {
VStack {
VStack {
if showImage {
Image(systemName: "house.fill").resizable().aspectRatio(contentMode: .fill).frame(width: 75, height: 75)
Text("TEST")
}
}.frame(height: 100)
Spacer().frame(height: 100)
Button(action: { showImage.toggle() }) {
Text("TEST").foregroundColor(Color.white).padding()
}.background(Color.black) .cornerRadius(10) }
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346658.html
