我正在尝试为弹出式窗口设置一定的大小或使其适应其内容
我尝试从弹出框更改视图的框架,但似乎不起作用
Button("Popover") {
self.popover7.toggle()
}.popover(isPresented: self.$popover7, arrowEdge: .bottom) {
PopoverView().frame(width: 100, height: 100, alignment: .center)
}
我想实现我在iPad的“日历”应用中发现的这种行为
答案 0 :(得分:3)
我使用自定义UIViewRepresentable
在iOS上运行它。用法如下:
struct Content: View {
@State var open = false
@State var popoverSize = CGSize(width: 300, height: 300)
var body: some View {
WithPopover(
showPopover: $open,
popoverSize: popoverSize,
content: {
Button(action: { self.open.toggle() }) {
Text("Tap me")
}
},
popoverContent: {
VStack {
Button(action: { self.popoverSize = CGSize(width: 300, height: 600)}) {
Text("Increase size")
}
Button(action: { self.open = false}) {
Text("Close")
}
}
})
}
}
还有here is a gist和WithPopover
的来源
答案 1 :(得分:0)
仅macOS
这里是如何动态更改弹出框的方法...为简单起见,它没有动画,由您决定。
struct TestCustomSizePopover: View {
@State var popover7 = false
var body: some View {
VStack {
Button("Popover") {
self.popover7.toggle()
}.popover(isPresented: self.$popover7, arrowEdge: .bottom) {
PopoverView()
}
}.frame(width: 800, height: 600)
}
}
struct PopoverView: View {
@State var adaptableHeight = CGFloat(100)
var body: some View {
VStack {
Text("Popover").padding()
Button(action: {
self.adaptableHeight = 300
}) {
Text("Button")
}
}
.frame(width: 100, height: adaptableHeight)
}
}