我创建了一个这样的 ShareButton:
struct ShareButton<Content: View>: View {
var items: [Any]
var content: () -> Content
var body: some View {
Button {
let avc = UIActivityViewController(activityItems: items, applicationActivities: [])
avc.popoverPresentationController?.sourceView = UIHostingController(rootView: self).view
UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController?.present(avc, animated: false)
} label: {
content()
}
}
}
可以这样使用:
ShareButton(items: [someURL]){
Label("share", systemImage:"square.and.arrow.up")
}
但是,在 Mac(Catalyst) 和 iPad 上,共享菜单弹出窗口出现在错误的位置。
似乎 UIHostingController(rootView: self).view
返回了错误的参考视图。有人知道如何解决这个问题吗?