我可以为 UIMenu 设置固定顺序吗?

问题描述 投票:0回答:1

我的应用程序中有一个 UIMenu,我正在按特定顺序填充它。我希望菜单始终按照我填充的顺序显示。

但是,UIMenu 的自动行为会以此顺序作为“优先”顺序显示菜单。这意味着,它有时会更改列表的顺序,以将第一个项目(最高优先级)定位到最接近打开菜单的按钮。

因此,通过这种默认行为,我看到我的列表有时以相反的顺序显示。

我知道 UIContextMenu 有一个 .fixed 选项,可以使用 menu.preferredMenuElementOrder 设置。不过,我没有看到 UIMenu 的任何类似内容。

关于如何将菜单顺序设置为 .fixed 或防止这种自动排序行为有什么想法吗?

这是一个代码片段,显示我如何在自定义文本字段类中创建和填充菜单:

    private var dropdownMenu = UIMenu() {
    didSet {
        dropdownButton.menu = dropdownMenu
    }
}

/// configureDropdownMenu() is used to set the menu contents for your dropdown textfield.
/// - Parameter viewModel: Must set the data of your ViewModel in the View layer. The ViewModel data will be used to populate the dropdown menu.
func configureDropdownMenu(with viewModel: CustomPulldownTextFieldViewModel) {
    // Set the field type and the delegate
    self.customFieldType = viewModel.dropdownType
    self.isEnabled = true

    var dropdownOptions: [String: [UIAction]] = [:]
    var dropdownChildren: [UIMenuElement] = []
    
    switch viewModel.dropdownType {
    case .categorized:
        guard let dropdownHeaders = viewModel.pulldownHeaders else {
            fatalError("\nFatal Error: Must pass in headers for categorized dropdown type\n")
        }
        
        for (index, section) in viewModel.sectionOptions.enumerated() {
            let sectionHeader = dropdownHeaders[index]
            dropdownOptions[sectionHeader] = section.map { title in
                UIAction(title: title, state: .off) { [self] _ in
                    textField.text = title
                    self.delegate?.selectedOptionFromList(pulldownField: self, selection: title)
                }
            }
        }
        
        for header in dropdownHeaders {
            // Check if the section is not empty before adding it
            if let sectionActions = dropdownOptions[header], !sectionActions.isEmpty {
                dropdownChildren.append(UIAction(title: header, attributes: [.disabled], state: .off, handler: { _ in }))
                dropdownChildren.append(contentsOf: sectionActions)
            }
        }
        
        let categorizedMenu = UIMenu(options: .singleSelection, children: dropdownChildren)
        self.dropdownMenu = categorizedMenu
        
    case .uncategorized:
        // Uncategorized list MUST only have one section in sectionOptions
        dropdownChildren = viewModel.sectionOptions[0].map { title in
            UIAction(title: title, state: .off) { [self] _ in
                textField.text = title
                self.delegate?.selectedOptionFromList(pulldownField: self, selection: title)
            }
        }
        
        let uncategorizedMenu = UIMenu(options: .displayInline, children: dropdownChildren)
        self.dropdownMenu = uncategorizedMenu
    case .none:
        fatalError("\nFatal Error: Must set a dropdownType in the view model\n")
    }
}
swift uikit uibutton dropdown uimenu
1个回答
0
投票

由于此菜单是添加到按钮上的,因此您应该设置 preferredMenuElementOrder

UIButton
 属性。

dropdownButton.menu = dropdownMenu
dropdownButton.preferredMenuElementOrder = .fixed
© www.soinside.com 2019 - 2024. All rights reserved.