Swift ui 按钮或图像上的 macOS 弹出窗口
Swift ui macOS popover on the button or on an image
我希望我能做到图片中看到的那样。
我正在尝试使用这段代码,但不太了解如何修复它。
我希望我可以在 Button
或应该是图像的 ImageMac
上使用它。
谁能帮帮我?
代码:
func showLittlePopoverWithMessage(sender: NSView, message: String) {
let controller = NSViewController()
controller.view = NSView(frame: CGRect(x: CGFloat(100), y: CGFloat(50), width: CGFloat(100), height: CGFloat(50)))
let popover = NSPopover()
popover.contentViewController = controller
popover.contentSize = controller.view.frame.size
popover.behavior = .transient
popover.animates = true
let invisibleWindow = NSWindow(contentRect: NSMakeRect(0, 0, 20, 5), styleMask: .borderless, backing: .buffered, defer: false)
invisibleWindow.backgroundColor = .red
invisibleWindow.alphaValue = 0
//controller.view.addSubview(sender)
popover.show(relativeTo: sender.bounds, of: sender as! NSView, preferredEdge: .maxY)
}
#if os(macOS)
struct ImageMac: View {
let symbol: String
init(systemName: String) {
self.symbol = [
"star": "☆",
"star.fill": "★",
"heart": "",
"heart.fill": "",
"video": "",
"lock.fill": "",
"lock.open.fill": "",
"checkmark.seal.fill": ""
][systemName] ?? "?"
}
var body: some View { Text(symbol) }
}
#endif
ImageMac(systemName: "checkmark.seal.fill").foregroundColor(.blue)
Button(action: {}) {
Text("Button")
}
这是可能的方法演示。使用 Xcode 11.7 / macOS 10.15.6
准备和测试
struct ContentView: View {
@State var isPopover = false
var body: some View {
VStack {
Button(action: { self.isPopover.toggle() }) {
Image(nsImage: NSImage(named: NSImage.infoName) ?? NSImage())
}.popover(isPresented: self.$isPopover, arrowEdge: .bottom) {
PopoverView()
}.buttonStyle(PlainButtonStyle())
}.frame(width: 800, height: 600)
}
}
struct PopoverView: View {
var body: some View {
VStack {
Text("Some text here ").padding()
Button("Resume") {
}
}.padding()
}
}
我希望我能做到图片中看到的那样。
我正在尝试使用这段代码,但不太了解如何修复它。
我希望我可以在 Button
或应该是图像的 ImageMac
上使用它。
谁能帮帮我?
代码:
func showLittlePopoverWithMessage(sender: NSView, message: String) {
let controller = NSViewController()
controller.view = NSView(frame: CGRect(x: CGFloat(100), y: CGFloat(50), width: CGFloat(100), height: CGFloat(50)))
let popover = NSPopover()
popover.contentViewController = controller
popover.contentSize = controller.view.frame.size
popover.behavior = .transient
popover.animates = true
let invisibleWindow = NSWindow(contentRect: NSMakeRect(0, 0, 20, 5), styleMask: .borderless, backing: .buffered, defer: false)
invisibleWindow.backgroundColor = .red
invisibleWindow.alphaValue = 0
//controller.view.addSubview(sender)
popover.show(relativeTo: sender.bounds, of: sender as! NSView, preferredEdge: .maxY)
}
#if os(macOS)
struct ImageMac: View {
let symbol: String
init(systemName: String) {
self.symbol = [
"star": "☆",
"star.fill": "★",
"heart": "",
"heart.fill": "",
"video": "",
"lock.fill": "",
"lock.open.fill": "",
"checkmark.seal.fill": ""
][systemName] ?? "?"
}
var body: some View { Text(symbol) }
}
#endif
ImageMac(systemName: "checkmark.seal.fill").foregroundColor(.blue)
Button(action: {}) {
Text("Button")
}
这是可能的方法演示。使用 Xcode 11.7 / macOS 10.15.6
准备和测试struct ContentView: View {
@State var isPopover = false
var body: some View {
VStack {
Button(action: { self.isPopover.toggle() }) {
Image(nsImage: NSImage(named: NSImage.infoName) ?? NSImage())
}.popover(isPresented: self.$isPopover, arrowEdge: .bottom) {
PopoverView()
}.buttonStyle(PlainButtonStyle())
}.frame(width: 800, height: 600)
}
}
struct PopoverView: View {
var body: some View {
VStack {
Text("Some text here ").padding()
Button("Resume") {
}
}.padding()
}
}