对于这样的视图,我有一个contextMenu视图修饰符:
Text("Some Text")
.contextMenu {
Button(action: {
editCodes(withTappedCode: codeOnDisplay, delete: true)
}, label: {
Text("Paste")
Image(systemName: "doc.on.clipboard")
})
.disabled(!UIPasteboard.general.contains(pasteboardTypes: [aPAsteBoardType]))
}仅当某个粘贴板类型可用时,才应启用该按钮。然而,这并不会发生。
禁用状态是在按钮的上下文菜单首次显示时设置的。在此之后,对粘贴板的任何更改都不会修改禁用状态,即使菜单已关闭并再次打开也是如此。
这似乎只有在以任何方式刷新修改后的视图时才会发生。
如何使用粘贴板类型更改上下文菜单按钮的禁用状态?
发布于 2020-10-28 04:33:13
您可以监听UIPasteboard.changedNotification以检测更改并刷新视图:
struct ContentView: View {
@State private var pasteDisabled = false
var body: some View {
Text("Some Text")
.contextMenu {
Button(action: {}) {
Text("Paste")
Image(systemName: "doc.on.clipboard")
}
.disabled(pasteDisabled)
}
.onReceive(NotificationCenter.default.publisher(for: UIPasteboard.changedNotification)) { _ in
pasteDisabled = !UIPasteboard.general.contains(pasteboardTypes: [aPAsteBoardType])
}
}
}(您可能还想使用UIPasteboard.removedNotification)。
https://stackoverflow.com/questions/64562348
复制相似问题