ボタンまたは画像上の SWIFT UI MacOS ポップアップ

写真にあるようなことができればいいのですが。

このコードを使おうとしているのですが、修正方法がよくわかりません。

画像であるはずの 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()

    }

}

さて、ボタンまたは画像上の SWIFT UI MacOS ポップアップ ウィンドウに関するチュートリアルはこれで終わりです。楽しいテンプレート ソース コード ネットワークにあるこの技術記事が役立つことを願っています。サイト内でさらに技術的なチュートリアルを検索できます。 。

おすすめ

転載: blog.csdn.net/weixin_42610770/article/details/131058835