D. *_*ika 2 macos combobox swiftui
有没有办法在 SwiftUI 中创建一个NSComboBox?当我使用时Picker,NSPopUpButton会在MacOS下创建一个。
我试过这个:
var body: some View {
    Picker("Test:", selection: $selected) {
        ForEach(1..<10) { i in
            Text(String(i))
        }
    }
    .padding()
}
但这会让我得到这个:
但我需要一个组合框,可以在其中输入文本并从列表中进行选择。
在不集成NSComboBoxas 的情况下,这对于 SwiftUI 来说是否可能NSViewRepresentable?
我已经检查过以下问题:SwiftUI Custom Picker / ComboBox 但它是关于自定义选择器而不是关于 NSComboBox。
使用起来NSViewRepresentable并没有我想象的那么糟糕。NSComboBox下面是SwiftUI的完整实现:
import Foundation
import SwiftUI
struct VDKComboBox: NSViewRepresentable
{
    // The items that will show up in the pop-up menu:
    var items: [String]
    
    // The property on our parent view that gets synced to the current stringValue of the NSComboBox, whether the user typed it in or selected it from the list:
    @Binding var text: String
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    
    func makeNSView(context: Context) -> NSComboBox
    {
        let comboBox = NSComboBox()
        comboBox.usesDataSource = false
        comboBox.completes = false
        comboBox.delegate = context.coordinator
        comboBox.intercellSpacing = NSSize(width: 0.0, height: 10.0)            // Matches the look and feel of Big Sur onwards.
        return comboBox
    }
    
    func updateNSView(_ nsView: NSComboBox, context: Context)
    {
        nsView.removeAllItems()
        nsView.addItems(withObjectValues: items)
        
        // ComboBox doesn't automatically select the item matching its text; we must do that manually. But we need the delegate to ignore that selection-change or we'll get a "state modified during view update; will cause undefined behavior" warning.
        context.coordinator.ignoreSelectionChanges = true
        nsView.stringValue = text
        nsView.selectItem(withObjectValue: text)
        context.coordinator.ignoreSelectionChanges = false
    }
}
// MARK: - Coordinator
extension VDKComboBox
{
    class Coordinator: NSObject, NSComboBoxDelegate
    {
        var parent: VDKComboBox
        var ignoreSelectionChanges: Bool = false
        
        init(_ parent: VDKComboBox) {
            self.parent = parent
        }
        
        func comboBoxSelectionDidChange(_ notification: Notification)
        {
            if !ignoreSelectionChanges,
               let box: NSComboBox = notification.object as? NSComboBox,
               let newStringValue: String = box.objectValueOfSelectedItem as? String
            {
                parent.text = newStringValue
            }
        }
        
        
        func controlTextDidEndEditing(_ obj: Notification)
        {
            if let textField = obj.object as? NSTextField
            {
                parent.text = textField.stringValue
            }
        }
    }
}
struct MyView: View
{   
    @State var comboBoxText: String = ""
    var body: some View
    {
         VDKComboBox(items: ["one", "two", "three"], text: $comboBoxText)
             .padding()
    }
}
当然,您将动态填充项目列表;硬编码的字符串数组只是一个简单的例子。
我确实意识到你明确排除了NSViewRepresentable,但我尝试了很多其他方法来模仿NSComboBox,包括:
ButtonTextField 顶部的SwiftUI ,.trailing对齐TextField和PickerPopover链接到的自定义TextField所有这些在某种程度上看起来都是“外国的”。因为NSViewRepresentable事情并没有那么糟糕,所以我就这么决定了。它可能会在将来帮助其他人,所以我把我的实现放在这里,即使你想要一个不依赖于NSViewRepresentable.
| 归档时间: | 
 | 
| 查看次数: | 2542 次 | 
| 最近记录: |