xcode 有没有办法在 SwiftUI 中创建下拉菜单/按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56513339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to create a Dropdown-Menu/button in SwiftUI?
提问by Libiph
I want to create a dropdown-menu in Xcode 11 Beta 1. But i have not found a way to to it in iOS.
我想在 Xcode 11 Beta 1 中创建一个下拉菜单。但我还没有在 iOS 中找到它的方法。
I have tried it with the .hidden function and found the PullDownButton, but don‘t know how to set it up
我已经用 .hidden 函数试过了,找到了 PullDownButton,但不知道如何设置
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
但是找不到一个是为隐藏的按钮设置“弹出”动画并希望主按钮保持在其位置
回答by Libiph
回答by FlowUI. SimpleUITesting.com
Text("Options")
.contextMenu {
Button(action: {
// change country setting
}) {
Text("Choose Country")
}
Button(action: {
// enable geolocation
}) {
Text("Detect Location")
}
}
taken from https://www.hackingwithswift.com/quick-start/swiftui/how-to-show-a-context-menuRight click it to show the view
取自https://www.hackingwithswift.com/quick-start/swiftui/how-to-show-a-context-menu右键单击它以显示视图
回答by Joshua Jiang
You need to use an overlay to display your dropdown. Otherwise, parents' layout will be wrong when you show and hide the dropdown.
您需要使用叠加层来显示您的下拉列表。否则,当您显示和隐藏下拉菜单时,父母的布局将是错误的。
Here is a simple answer, and the complete answer could be found here
这是一个简单的答案,完整的答案可以在这里找到
struct Dropdown: View {
var options: [DropdownOption]
var onSelect: ((_ key: String) -> Void)?
var body: some View {
VStack(alignment: .leading, spacing: 0) {
ForEach(self.options, id: \.self) { option in
DropdownOptionElement(val: option.val, key: option.key, onSelect: self.onSelect)
}
}
.background(Color.white)
.cornerRadius(dropdownCornerRadius)
.overlay(
RoundedRectangle(cornerRadius: dropdownCornerRadius)
.stroke(Color.coreUIPrimary, lineWidth: 1)
)
}
}
struct DropdownButton: View {
@State var shouldShowDropdown = false
@Binding var displayText: String
var options: [DropdownOption]
var onSelect: ((_ key: String) -> Void)?
let buttonHeight: CGFloat = 30
var body: some View {
Button(action: {
self.shouldShowDropdown.toggle()
}) {
HStack {
Text(displayText)
Spacer()
.frame(width: 20)
Image(systemName: self.shouldShowDropdown ? "chevron.up" : "chevron.down")
}
}
.padding(.horizontal)
.cornerRadius(dropdownCornerRadius)
.frame(height: self.buttonHeight)
.overlay(
RoundedRectangle(cornerRadius: dropdownCornerRadius)
.stroke(Color.coreUIPrimary, lineWidth: 1)
)
.overlay(
VStack {
if self.shouldShowDropdown {
Spacer(minLength: buttonHeight + 10)
Dropdown(options: self.options, onSelect: self.onSelect)
}
}, alignment: .topLeading
)
.background(
RoundedRectangle(cornerRadius: dropdownCornerRadius).fill(Color.white)
)
}
}