更新到Xcode beta-3,Popover被弃用.花了很长时间想办法让它再起作用!
这是旧的密码,非常有效.
struct ExerciseFilterBar : View {
@Binding var filter: Exercise.Filter
@State private var showPositions = false
var body: some View {
HStack {
Spacer()
Button(action: { self.showPositions = true } ) {
Text("Position")
}
.presentation(showPositions ? Popover(content: MultiPicker(items: Exercise.Position.allCases, selected:$filter.positions),
dismissHandler: { self.showPositions = false })
: nil)
}
.padding()
}
}这是新密码..。
struct ExerciseFilterBar : View {
@Binding var filter: Exercise.Filter
@State private var showPositions = false
var body: some View {
HStack {
Spacer()
Button(action: { self.showPositions = true } ) {
Text("Position")
}
.popover(isPresented: $showPositions) {
MultiPicker(items: Exercise.Position.allCases, selected:self.$filter.positions)
.onDisappear { self.showPositions = false }
}
}
.padding()
}
}最后我使用了PresentationLink,这样我就可以继续做其他的事情了.
struct ExerciseFilterBar : View {
@Binding var filter: Exercise.Filter
var body: some View {
HStack {
Spacer()
PresentationLink(destination: MultiPicker(items: Exercise.Position.allCases, selected:$filter.positions)) {
Text("Position")
}
}
.padding()
}
}就测试而言,它是有效的,但它不是一个弹出式的。
谢谢你的建议!
顺便说一句,这段代码正在iPad模拟器中。
发布于 2019-07-08 07:54:26
在OSX上,下面的代码运行良好
struct ContentView : View {
@State var poSelAbove = false
@State var poSelBelow = false
@State var pick : Int = 1
var body: some View {
let picker = Picker(selection: $pick, label: Text("Pick option"), content:
{
Text("Option 0").tag(0)
Text("Option 1").tag(1)
Text("Option 2").tag(2)
})
let popoverWithButtons =
VStack {
Button("Not Dismiss") {
}
Divider()
Button("Dismiss") {
self.poSelAbove = false
}
}
.padding()
return VStack {
Group {
Button("Show button popover above") {
self.poSelAbove = true
}.popover(isPresented: $poSelAbove, arrowEdge: .bottom) {
popoverWithButtons
}
Divider()
Button("Show picker popover below") {
self.poSelBelow = true
}.popover(isPresented: $poSelBelow, arrowEdge: .top) {
Group {
picker
}
}
}
Divider()
picker
.frame(width: 300, alignment: .center)
Text("Picked option: \(self.pick)")
.font(.subheadline)
}
// comment the line below for iOS
.frame(width: 800, height: 600)
}在iOS (iPad)上,弹出器将以奇怪的透明全屏模式出现。我不认为这是故意的。我已经将这个问题添加到我现有的bug报告中。
https://stackoverflow.com/questions/56905648
复制相似问题