Home>

I would like to have a function to show or hide sentences when you press the "Show" or "Hide" button like the above image. Please tell me how to implement it in Swift UI. Can you get it?
The thing in the image uses storyboard, and I would like to implement it in Swift UI.
I tried to google on the net, but I couldn't find it easily. Can anyone tell me?

  • Answer # 1

    I think this area is basic, but the following is an example.
    Just look at the tapped state of the button and sort out.
    I want to switch the display in real time, so I use @State.

    import SwiftUI
    struct ContentView: View {
        @State private var isPushed = false
        var body: some View {
            ScrollView {
                VStack {
                    HStack {
                        Text ("weather")
                            .font (.headline)
                        Button (action: {
                            // Switch here
                            self.isPushed.toggle ()
                        }, label: {
                            Text (isPushed? "Show": "Hide")
                        })
                        Spacer ()
                    }
                    .padding (.leading)
                    if! isPushed {
                        Text ("Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather Information Weather information")
                        .padding ()
                    }
                    Spacer ()
                }
            }
        }
    }