简体   繁体   中英

Picture and text when clicking on the button. SwiftUI

How to make a picture and text appear when you click on the button? 图片

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)
}

I'm new to swift, Thanks for any help

You can use an if clause to conditionally display elements based on a @State variable.

I'm using the extra frame modifier on the VStack that you didn't originally have to make sure that nothing changes position when the @State variable changes and the elements inside the stack appear/disappear.

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) }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM