語境:
在 macOS 12.0 上的 SwiftUI 中,我展示了一個像這樣的 Popover:

Button {
isShowingFilterPopover.toggle()
} label: {
...
}
.buttonStyle(.plain)
.popover(isPresented: $isShowingFilterPopover, attachmentAnchor: .point(.bottomTrailing), arrowEdge: .bottom, content: { ... })
問題:
我想告訴 SwiftUI 將此彈出框錨定在按鈕.bottomTrailing位置左側的 8 點處。
我知道我可以指定一個精確的點作為按鈕的偏移量,如下所示:
attachmentAnchor: .rect(.rect(CGRect(x: 70, y: 15, width: 0, height: 0)))
但這適用于前(左)邊緣按鈕的框架,我需要從其后(右)邊緣作業。原因:按鈕的文本(“所有條目”)會動態變化以顯示用戶輸入的任何搜索詞,這意味著按鈕的寬度會發生變化。因此,不可能從前緣以恒定偏移量進行加工。
那么,如何告訴 SwiftUI 將彈出框 8 點錨定到按鈕后沿左側?
uj5u.com熱心網友回復:
只需為彈出框提供 8px 的小視圖,其他一切都會自動發生。
這是一個演示。使用 Xcode 13.4 / macOS 12.4 測驗

Button { self.isPop.toggle() }
label: { Text("Pop").padding() }
.buttonStyle(.plain)
.padding(.horizontal, -8) // << here !!
.popover(isPresented: $isPop,
attachmentAnchor: .point(.bottomTrailing),
arrowEdge: .bottom) { ... }
.padding(.horizontal, 8)
GitHub 上的測驗模塊
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495247.html
