UIMenu | 如何更改文本和背景颜色 UIAction(Swift 5)

Iam*_*SSR 13 uikit uibarbuttonitem swift uimenu uiaction

请告诉我如何定制这个菜单?也许还有另一种方法可以做到这一点?

用户界面菜单

let barMenu = UIMenu(title: "", children: [
        UIAction(title: NSLocalizedString("menu_item_home", comment: "")){
                                action in
                                print("menu_item_home 1")
                            },
                            UIAction(title: NSLocalizedString("menu_item_settings", comment: "")){
                                action in
                                print("menu_item_settings 2")
                                
                                 let settingsStoryboard = UIStoryboard(name: "Settings", bundle: nil)
                                 let settingsController = settingsStoryboard.instantiateViewController(withIdentifier: "SettingsScene") as! SettingsViewController
                                 controller.navigationController?.pushViewController(settingsController, animated: true)
                                 
                            },
        
                            UIAction(title: NSLocalizedString("menu_item_contacts", comment: "")){
                                action in
                                print("menu_item_contacts 3")
                                
                                
                            },
                        
    ])
    
    let navBarMenu = UIBarButtonItem(image: UIImage(systemName: "text.justify"), menu: barMenu)
    navigationItem.rightBarButtonItem = navBarMenu
Run Code Online (Sandbox Code Playgroud)

我需要向导航栏添加一个菜单并自定义其外观。请指出正确的方向

Les*_*ary 6

不幸的UIMenu是,它不是一个UIView,并且自定义它的选项很少。至少在当前的 iOS 15 中无法更改背景或文本颜色,并且至少不需要采取一些愚蠢的解决方法。如果您需要在此菜单中具有不同的外观,那么也许您可以使用自定义弹出窗口而不是使用 UIMenu 创建类似的内容。这将为您提供更多的定制选项。不完全符合您的要求,但也许它会满足您的需求。基于弹出窗口的类似菜单的可能实现可能或多或少如下所示:

class MenuViewController: UITableViewController, UIPopoverPresentationControllerDelegate {
    private var actions: [UIAction] = [] // or perhaps something custom

    convenience init(actions: [UIAction]) {
        self.init(style: .plain)
        self.actions = actions
        modalPresentationStyle = .popover
        preferredContentSize = CGSize(width: 240, height: 40 * actions.count)
        presentationController?.delegate = self
        popoverPresentationController?.permittedArrowDirections = .up
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        tableView.rowHeight = 40
        tableView.separatorInset = .zero
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return actions.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        let action = actions[indexPath.row]
        cell.textLabel?.text = action.title
        cell.imageView?.image = action.image
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // do something
    }

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将其从代码中的某个位置呈现出来,如下所示:

    let menuVC = MenuViewController(actions: [
        // actions
    ])
    menuVC.popoverPresentationController?.sourceView = mySourceView
    menuVC.popoverPresentationController?.sourceRect = mySourceView.bounds
    present(menuVC, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)