简体   繁体   English

在 SwiftUI 中移动列表元素 position

[英]Move inside list element position in SwiftUI

I'm trying to move a list element position when i click in another one当我单击另一个列表元素时,我正在尝试移动列表元素 position

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.由于您已经预定义了元素数量,因此在这里使用ScrollView而不是List更合适。 So solution can be as follows (in pseudo code)所以解决方案可以如下(在伪代码中)

Update with full demo code.更新完整的演示代码。 Tested with Xcode 11.4 / iOS 13.4使用 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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