gpt4 book ai didi

SwiftUI:调整弹出窗口的大小以适合

转载 作者:行者123 更新时间:2023-12-03 09:18:01 25 4
gpt4 key购买 nike

我有一个弹出窗口示例,其中一个按钮触发了一个弹出窗口。弹出窗口仅包含少量UI,在这种情况下为两个按钮,但是它仍然占用大量空间,而不是像我从UIKit习惯的那样将内容整齐地包裹在内容周围。如何使弹出窗口适合内容的大小?

iPad模拟器和以下代码的屏幕截图:

Screenshot of the button with the popover open

struct ContentView: View {

@State private var showingPopupA = false

var body: some View {
HStack {
Button(action: {
self.showingPopupA.toggle()
}, label: {
Text("Button")
}).popover(isPresented: self.$showingPopupA) {
VStack {
Button(action: {
// Do something
self.showingPopupA = false
}) {
Text("Option A")
}
Button(action: {
// Do something
self.showingPopupA = false
}) {
Text("Option B")
}
}.background(Color.red)
}
}
}
}

来自macOS的屏幕截图:
macOS screenshot built with Xcode 11.0

最佳答案

在macOS上,以下代码如下所示:

enter image description here

struct PopoverExample: View {

@State private var showingPopupA:Bool = false
var body: some View {
HStack {
Button(action: {
self.showingPopupA.toggle()
}, label: {
Text("Button")
}).popover(isPresented: self.$showingPopupA) {
VStack {
Button(action: {
// Do something
self.showingPopupA = false
}) {
Text("Option A")
}
Button(action: {
// Do something
self.showingPopupA = false
}) {
Text("Option B")
}
}.background(Color.red)
}
}
.frame( maxWidth: .infinity, maxHeight: .infinity)
}
}

项目 link

关于SwiftUI:调整弹出窗口的大小以适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58179317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com