Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to create a Dropdown-Menu/button in SwiftUI?

I want to create a dropdown-menu in Xcode 11 Beta 1. But i have not found a way to to it in iOS.

I have tried it with the .hidden function and found the PullDownButton, but don‘t know how to set it up

I have created this Code

struct SwiftUIView : View {
@State var array = true
@State var buttonTitle = "Zeige Deteils"

var body: some View {
    VStack {
        VStack {
            Button(action: {
                self.array.toggle()

            }) {
                Text(buttonTitle)
            }


            if array {
                VStack(spacing: 1.0) {

                    Button(action: {
                        self.buttonTitle = "Schmelzpunkt"
                        self.array.toggle()
                    }) {
                        Text("Schmelzpunkt")
                            .color(.white)
                            .padding(.all)
                    }
                    .background(Color.blue)
                    Button(action: {
                        self.buttonTitle = "Instrumentelle Analytik"
                        self.array.toggle()
                    }) {
                        Text("Instrumentelle Analytik")
                            .color(.white)
                            .padding(.all)
                            }.background(Color.blue)
                            Button(action: {
                                self.buttonTitle = "Aussehen"
                                self.array.toggle()
                            }) {
                                Text("Aussehen")
                                    .color(.white)
                                    .padding(.all)
                                    }.background(Color.blue)

                                }
                                .padding(.top)
                        }
                    }
                }
}

But can't find a was to animate the "poping-up" auf the hidden Buttons and want to the primary button to stay at its position

like image 422
Libiph Avatar asked Jun 09 '19 09:06

Libiph


People also ask

How do I add a button to a drop-down list?

Example Explained. Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

How do I create a side menu in SwiftUI?

Let's add the @State var to our ContentView . Now, let's create the sidebar view by creating a new SwiftUI file. Go to File → New → File or press ⌘ + N to create a new file. Select the type of file you want to create, select SwiftUI View, and click Next.


2 Answers

In SwiftUI 2.0 (iOS 14+) you can make a dropdown menu with Menu.

Menu {
    Button {
        style = 0
    } label: {
        Text("Linear")
        Image(systemName: "arrow.down.right.circle")
    }
    Button {
        style = 1
    } label: {
        Text("Radial")
        Image(systemName: "arrow.up.and.down.circle")
    }
} label: {
     Text("Style")
     Image(systemName: "tag.circle")
}
like image 75
Ben216k Avatar answered Oct 20 '22 22:10

Ben216k


Using SwiftUI 2.0 you can also implement dropdown menu with DisclosureGroup here is ref.

GroupBox {
    DisclosureGroup("Menu 1") {
        Text("Item 1")
        Text("Item 2")
        Text("Item 3")
    }
}
like image 40
atalayasa Avatar answered Oct 20 '22 23:10

atalayasa