我希望我能做图片中看到的事情。
我正在尝试使用这段代码,但我不太了解怎么修复它。
我希望我可以在Button或ImageMac上使用它,该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 = controllerpopover.contentSize = controller.view.frame.sizepopover.behavior = .transientpopover.animates = truelet invisibleWindow = NSWindow(contentRect: NSMakeRect(0, 0, 20, 5), styleMask: .borderless, backing: .buffered, defer: false)invisibleWindow.backgroundColor = .redinvisibleWindow.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: Stringinit(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) }}#endifImageMac(systemName: "checkmark.seal.fill").foregroundColor(.blue)Button(action: {}) {Text("Button")}
推荐答案
以下是可能的方法演示。编写并使用Xcode 11.7/MacOS 10.15.6进行了测试
struct ContentView: View {@State var isPopover = falsevar 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()}}
好了关于按钮或图像上的SWIFT UI MacOS弹出窗口的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。