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

12
请告诉我如何自定义这个菜单? 或许还有其他方法? UIMenu
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

我需要在NavigationBar中添加一个菜单并自定义其外观。请指点一下方向。

你找到方法了吗? - Jamil
你解决了吗? - pechr
有任何解决方案吗?苹果没有记录它。 - Omar N Shamali
1个回答

4

很遗憾,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
    }
}

然后在你的代码中像这样展示它:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接