SwiftUI 自定义选择器/组合框 [英] SwiftUI Custom Picker / ComboBox

查看:46
本文介绍了SwiftUI 自定义选择器/组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在适用于 MacOS 的 SwiftUI 中获取自定义选择器.我想要的最佳选择是自定义视图,可以按下该视图,然后显示像 AppKit 中的 ComboBox 一样的 Picker 选项.

I am trying to get a customized Picker in SwiftUI for MacOS. The best option I would like to have is a customized view which can be pressed and then shows the Picker options like a ComboBox in AppKit.

Apple 在 Mac 上的通讯录应用或 Apple 的共享对话框中实现了这一点.

Apple achieves that in their Contact books app on Mac or the Sharing dialog of Apple is the same way.

您按下 + 按钮,就会出现选择.这在 SwiftUI 中可能吗?

You press the + Button and the selection comes up. Is that possible in SwiftUI?

这看起来与 SwiftUI 中的 contextMenu 非常相似.但是我怎样才能在左键单击时设置它呢?

This looks very similar like contextMenu in SwiftUI. But how can I set it on left click instead?

推荐答案

终于找到了 MenuButton,它对 MacOS 中的 SwiftUI 起到了作用.

Finally found MenuButton which does the trick for SwiftUI in MacOS.

MenuButton(label: Title(), content: {
    Button(action: {
        print("Clicked an item")
    }) {
        Text("Menu Item Text")
    }
})
.menuButtonStyle(BorderlessButtonMenuButtonStyle())

... 为我的可点击按钮使用自定义视图 Title().

... with using a custom View Title() for my clickable button.

struct Title: View {
    var body: some View {
        HStack
        {
            Text("Title")
        }
    }
}

这篇关于SwiftUI 自定义选择器/组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆