Home > Net >  Is there a way to change a menu bar item to inactive state once clicked? Swift
Is there a way to change a menu bar item to inactive state once clicked? Swift

Time:08-11

Using the Swift built-in UIMenu with 3 UIActions. I'd like one of the actions to become inactive or at least grayed out once clicked. Is there a way? This is the code from Apple that I'm using in very similar way.

let barButtonMenu = UIMenu(title: "", children: [
    UIAction(title: NSLocalizedString("Copy", comment: ""), image: UIImage(systemName: "doc.on.doc"), handler: menuHandler),
    UIAction(title: NSLocalizedString("Rename", comment: ""), image: UIImage(systemName: "pencil"), handler: menuHandler),
    UIAction(title: NSLocalizedString("Duplicate", comment: ""), image: UIImage(systemName: "plus.square.on.square"), handler: menuHandler),
    UIAction(title: NSLocalizedString("Move", comment: ""), image: UIImage(systemName: "folder"), handler: menuHandler)
])
optionsBarItem.menu = barButtonMenu

CodePudding user response:

Use UIMenuElement.Attributes to disable the action after selection, in order to do that you are required to change your code accordingly.

    let a1 = UIAction(title: NSLocalizedString("Copy", comment: ""), image: UIImage(systemName: "doc.on.doc"), handler: menuHandler)
    let a2 = UIAction(title: NSLocalizedString("Rename", comment: ""), image: UIImage(systemName: "pencil"), handler: menuHandler)
    a2.attributes = .disabled // FIX
    let a3 = UIAction(title: NSLocalizedString("Duplicate", comment: ""), image: UIImage(systemName: "plus.square.on.square"), handler: menuHandler)
    let a4 = UIAction(title: NSLocalizedString("Move", comment: ""), image: UIImage(systemName: "folder"), handler: menuHandler)
    let barButtonMenu = UIMenu(title: "Actions", children: [a1, a2, a3, a4])

CodePudding user response:

let sort = UIAction(title: "Sort By Date",subtitle: "",attributes: .disabled) { _ in
                
 }

disable or enable the attributes

CodePudding user response:

Track selected menu and update attribute of menu accordingly, please refer below code:

private var selectedMenuItemTitle: String?
private var siteMenuItems:[UIMenuElement] = []

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    siteMenuItems = [
        UIAction(title: NSLocalizedString("Copy", comment: ""), image: UIImage(systemName: "doc.on.doc"), handler: menuHandler),UIAction(title: NSLocalizedString("Rename", comment: ""), image: UIImage(systemName: "pencil"), handler: menuHandler),UIAction(title: NSLocalizedString("Duplicate", comment: ""), image: UIImage(systemName: "plus.square.on.square"), handler: menuHandler),UIAction(title: NSLocalizedString("Move", comment: ""), image: UIImage(systemName: "folder"), handler: menuHandler)
    ]
    createSiteMenu(actionTitle: selectedMenuItemTitle)
}

private func createSiteMenu(actionTitle: String? = nil) {
    let menu = UIMenu(title: "Site", image: nil, identifier: nil, options: [], children: siteMenuItems)
    siteButton?.menu = updateActionState(actionTitle: actionTitle, menu: menu)
    siteButton?.showsMenuAsPrimaryAction = true
}

private func menuHandler(action:UIAction) -> () {
    selectedMenuItemTitle = action.title
    createSiteMenu(actionTitle: selectedMenuItemTitle)
}


private func updateActionState(actionTitle: String? = nil, menu: UIMenu) -> UIMenu {
    if let actionTitle = actionTitle {
        menu.children.forEach { action in
            guard let action = action as? UIAction else {
                return
            }
            if action.title == actionTitle {
                action.attributes = .disabled
            }
        }
    }
    return menu
}

Reference: https://developer.apple.com/documentation/uikit/uimenuelement/attributes

  • Related