简体   繁体   中英

Move inside list element position in SwiftUI

I'm trying to move a list element position when i click in another one

When i click thats what happen

I need the view to move up and stay like this without the user need to move it

        VStack{
            VStack{
                Button(action: {
                    self.iconeView.toggle()
                }) {
                    ZStack{
                        self.icone.renderingMode(.original)
                            .frame(width: UIScreen.main.bounds.width * 0.4, height: UIScreen.main.bounds.height * 0.2)
                    }
                }

            }//Photo
                .padding(.bottom, 54)
                .padding(.top, 42)

            List {

                VStack(alignment: .leading){
                    Text("Nome do remédio")
                }//Medicine Name
                .padding(.bottom, 10)

                VStack(alignment: .leading){
                    Text("Quantidade de remédio")
                }// Quantity
                    .padding(.bottom, 50)

                VStack(alignment: .leading){
                    Text("Horario Inicial")

                }//Start time

            }//List


        }//Screen

As you have predefined number of elements it is more appropriate to use ScrollView instead of List here. So solution can be as follows (in pseudo code)

Update with full demo code. Tested with Xcode 11.4 / iOS 13.4

演示

struct DemoMoveToTopView: View {
    @State private var showMedicalName = true
    @State private var showQuantaty = true

    var body: some View {
        ScrollView {
            VStack {
            if self.showMedicalName {
                VStack(alignment: .leading){
                    Text("Nome do remédio")
                }//Medicine Name
                    .frame(width: 300, height: 100)
                    .border(Color.red).background(Color.yellow)
                    .padding(.bottom, 10)
                    .transition(.opacity)
            }

            if self.showQuantaty {
                VStack(alignment: .leading){
                    Text("Quantidade de remédio")
                }// Quantity
                    .frame(width: 300, height: 100)
                    .border(Color.red).background(Color.yellow)
                    .padding(.bottom, 50)
                    .transition(.opacity)
            }

            VStack(alignment: .leading){
                Text("Horario Inicial")

            }//Start time
                .frame(width: 300, height: 100)
                    .border(Color.red).background(Color.white)
                .onTapGesture {
                    self.showMedicalName.toggle()      // << hide/show above
                    self.showQuantaty.toggle()         // << hide/show above

                    // .. do other needed
            }

            }.animation(.default)
        }//ScrollView
    }
}

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