[英]SwiftUI Picker view without label
EDIT: I tried using the WheelPickerStyle outside of the form, however what I want is the same idea, the actual options instead of the label, but as a list.编辑:我尝试在表单之外使用 WheelPickerStyle,但是我想要的是相同的想法,实际选项而不是 label,而是作为列表。 Just like in my example with the Form, it's perfect except it has a label I have to click to get there.就像在我的表单示例中一样,它非常完美,除了它有一个 label 我必须单击才能到达那里。
I have an app which has a button that redirects me to another view like this:我有一个应用程序,它有一个按钮,可以将我重定向到另一个视图,如下所示:
Button(action: {
self.isAddDrinkView.toggle()
}) {
Image(systemName: "plus.circle")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 100, height: 100, alignment: .center)
.foregroundColor(.black)
.padding(.bottom, 50.0)
}
Now, this other view is supposed to be a list from where you could pick one, and only one, element.现在,这个另一个视图应该是一个列表,您可以从中选择一个,并且只能选择一个元素。
I tried doing it like this:我试着这样做:
struct AddDrinkView: View {
@ObservedObject var context: Context
var body: some View {
NavigationView{
Form{
Picker(selection: $context.selectedItems, label: Text("Select a drink"), content: {
ForEach(context.items){
Text("\($0)")
}
}).pickerStyle(DefaultPickerStyle())
.navigationBarTitle("Select a Drink")
}.labelsHidden()
}
}
}
However, my problem is that this shows me a view with a picker and a hidden label.但是,我的问题是这向我显示了一个带有选择器和隐藏 label 的视图。 I don't want this label at all, I'd prefer the picker options to show immediately when I enter this view, without having to click on a label.我根本不想要这个 label,我希望选择器选项在我进入这个视图时立即显示,而不必单击 label。
How can I achieve this?我怎样才能做到这一点? this is for ios 13+ Another option if possible was if I could somehow make the first button be the trigger for the picker options view, but I still don't know how to achieve this.这适用于 ios 13+ 如果可能的话,另一个选择是如果我能以某种方式使第一个按钮成为选择器选项视图的触发器,但我仍然不知道如何实现这一点。
This a way Form
presents default styled picker.这是Form
呈现默认样式选择器的一种方式。 Try to use instead WheelPickerStyle
尝试改用WheelPickerStyle
Picker(selection: $context.selectedItems, label: Text("Select a drink"), content: {
ForEach(context.items){
Text("\($0)")
}
}).pickerStyle(WheelPickerStyle()) // << here !!
You Can use Menu as Label您可以将菜单用作 Label
Menu("Drop Your Label Here") {
Picker("Please choose a color", selection: $selectedColor)
{
ForEach(colors, id: \.self) {
Text($0)
}
}
}
//.foregroundColor(Color.init("Red"))
.foregroundColor(Color.red)
}.frame(width: 300, height: 60)
You can set the label property when initialising to EmptyView()
.您可以在初始化为EmptyView()
时设置 label 属性。
Picker(selection: $context.selectedItems, label: EmptyView()) {
ForEach(colors, id: \.self) {
Text($0)
}
}
If you were to use an empty Text
for the label
property, it would show an empty bar, at least when using pickerStyle(.inline)
.如果您要为label
属性使用空Text
,它将显示一个空栏,至少在使用pickerStyle(.inline)
时。 If you use EmptyView
, there is no empty bar and you are able to display a picker without a label.如果您使用EmptyView
,则没有空栏,您可以在没有 label 的情况下显示选择器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.