SwiftUI 如何将完成按钮添加到选择器
SwiftUI How to add done button to picker
我制作了一个只有一个按钮和一个选择器的迷你应用程序,我的想法是在选择器上方有一个完成按钮,所以一旦我选择了一个值,我可以按下完成,选择器将关闭。
我知道如果您单击 "click me" 按钮,它会打开,如果您再次单击它,则会关闭选择器,但我正在寻找一个按钮,该按钮与选择器一起出现,并在单击时与答题器一起消失。
几乎就像选择器上方带有完成按钮的工具栏
@State var expand = false
@State var list = ["value1", "value2", "value3"]
@State var index = 0
var body: some View {
VStack {
Button(action: {
self.expand.toggle()
}) {
Text("Click me \(list[index])")
}
if expand {
Picker(selection: $list, label: EmptyView()) {
ForEach(0 ..< list.count) {
Text(self.list[[=10=]]).tag([=10=])
}
}.labelsHidden()
}
}
第三张图片是我想要完成的,前两张是我目前得到的
感谢您的帮助
向 if 子句添加一个按钮:
if expand {
VStack{
Button(action:{self.expand = false}){
Text("Done")
}
Picker(selection: $list, label: EmptyView()) {
ForEach(0 ..< list.count) {
Text(self.list[[=10=]]).tag([=10=])
}
}.labelsHidden()
}
}
这里有一个方法我会怎么做...当然调整仍然是可能的(动画,矩形等),但想法的方向应该是明确的
结果演示:
代码:
struct ContentView: View {
@State var expand = false
@State var list = ["value1", "value2", "value3"]
@State var index = 0
var body: some View {
VStack {
Button(action: {
self.expand.toggle()
}) {
Text("Click me \(list[index])")
}
if expand {
Picker(selection: $index, label: EmptyView()) {
ForEach(0 ..< list.count) {
Text(self.list[[=10=]]).tag([=10=])
}
}.labelsHidden()
.overlay(
GeometryReader { gp in
VStack {
Button(action: {
self.expand.toggle()
}) {
Text("Done")
.font(.system(size: 42))
.foregroundColor(.red)
.padding(.vertical)
.frame(width: gp.size.width)
}.background(Color.white)
Spacer()
}
.frame(width: gp.size.width, height: gp.size.height - 12)
.border(Color.black, width: 8)
}
)
}
}
}
}
这也行得通(特别是如果您在 Vstack 之外进行)...
// Toolbar for "Done"
func createToolbar() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
// "Done" Button for Toolbar on Picker View
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target:
self, action: #selector(PageOneViewController.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
// Makes Toolbar Work for Text Fields
familyPlansText.inputAccessoryView = toolBar
kidsOptionText.inputAccessoryView = toolBar
ethnicityText.inputAccessoryView = toolBar
}
并确保调用 createToolbar() 就大功告成了!
struct ContentView: View {
var colors = ["Red", "Green", "Blue"]
@State private var selectedColor = 0
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $selectedColor, label: Text("Color")) {
ForEach(0 ..< colors.count) {
Text(self.colors[[=10=]])
}
}
}
}
}
}
}
这有一些特定于表单的选择器行为,它以内联方式打开而无需隐藏按钮。
我制作了一个只有一个按钮和一个选择器的迷你应用程序,我的想法是在选择器上方有一个完成按钮,所以一旦我选择了一个值,我可以按下完成,选择器将关闭。
我知道如果您单击 "click me" 按钮,它会打开,如果您再次单击它,则会关闭选择器,但我正在寻找一个按钮,该按钮与选择器一起出现,并在单击时与答题器一起消失。
几乎就像选择器上方带有完成按钮的工具栏
@State var expand = false
@State var list = ["value1", "value2", "value3"]
@State var index = 0
var body: some View {
VStack {
Button(action: {
self.expand.toggle()
}) {
Text("Click me \(list[index])")
}
if expand {
Picker(selection: $list, label: EmptyView()) {
ForEach(0 ..< list.count) {
Text(self.list[[=10=]]).tag([=10=])
}
}.labelsHidden()
}
}
第三张图片是我想要完成的,前两张是我目前得到的
感谢您的帮助
向 if 子句添加一个按钮:
if expand {
VStack{
Button(action:{self.expand = false}){
Text("Done")
}
Picker(selection: $list, label: EmptyView()) {
ForEach(0 ..< list.count) {
Text(self.list[[=10=]]).tag([=10=])
}
}.labelsHidden()
}
}
这里有一个方法我会怎么做...当然调整仍然是可能的(动画,矩形等),但想法的方向应该是明确的
结果演示:
代码:
struct ContentView: View {
@State var expand = false
@State var list = ["value1", "value2", "value3"]
@State var index = 0
var body: some View {
VStack {
Button(action: {
self.expand.toggle()
}) {
Text("Click me \(list[index])")
}
if expand {
Picker(selection: $index, label: EmptyView()) {
ForEach(0 ..< list.count) {
Text(self.list[[=10=]]).tag([=10=])
}
}.labelsHidden()
.overlay(
GeometryReader { gp in
VStack {
Button(action: {
self.expand.toggle()
}) {
Text("Done")
.font(.system(size: 42))
.foregroundColor(.red)
.padding(.vertical)
.frame(width: gp.size.width)
}.background(Color.white)
Spacer()
}
.frame(width: gp.size.width, height: gp.size.height - 12)
.border(Color.black, width: 8)
}
)
}
}
}
}
这也行得通(特别是如果您在 Vstack 之外进行)...
// Toolbar for "Done"
func createToolbar() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
// "Done" Button for Toolbar on Picker View
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target:
self, action: #selector(PageOneViewController.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
// Makes Toolbar Work for Text Fields
familyPlansText.inputAccessoryView = toolBar
kidsOptionText.inputAccessoryView = toolBar
ethnicityText.inputAccessoryView = toolBar
}
并确保调用 createToolbar() 就大功告成了!
struct ContentView: View {
var colors = ["Red", "Green", "Blue"]
@State private var selectedColor = 0
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $selectedColor, label: Text("Color")) {
ForEach(0 ..< colors.count) {
Text(self.colors[[=10=]])
}
}
}
}
}
}
}
这有一些特定于表单的选择器行为,它以内联方式打开而无需隐藏按钮。