SwiftUI:调整弹出窗口的大小以适合 [英] SwiftUI: Sizing a popover to fit
本文介绍了SwiftUI:调整弹出窗口的大小以适合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个弹出窗口示例,其中一个按钮触发了一个弹出窗口.弹出窗口仅包含少量UI,在这种情况下为两个按钮,但是它仍然占用大量空间,而不是像我从UIKit习惯的那样将内容整齐地包裹在内容周围.如何使弹出窗口适合内容的大小?
I've got a little popover sample in which a button triggers a popover. The popover only contains a little bit of UI, two buttons in this case, but it still takes up a lot of space instead of wrapping neatly around the content like I'm used to from UIKit. How do I make the popover fit to the size of the content?
iPad模拟器的屏幕截图和以下代码:
Screenshot from the iPad simulator and code below:
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的屏幕截图:
Screenshot from macOS:
推荐答案
在macOS上,以下代码如下:
On macOS the code below will look like this:
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)
}
}
项目链接
这篇关于SwiftUI:调整弹出窗口的大小以适合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文