iosswiftuicontextualaction

Disable UIContextualAction


I am trying to disable a UIContextualAction that I have:

let picture = UIContextualAction(style: .normal, title:  " 📷 ", handler: { (ac: UIContextualAction, view: UIView, success:(Bool) -> Void) in
    // ...
})
picture.backgroundColor = .blue

I cant find anything online to enable or disable it, I still want it present, just don't want the user to tap on it. Is this possible?


Solution

  • I'm not sure it can be disabled out-of-the-box, but as a workaround, you could have a global variable keeping track whether the action should be enabled and then check that in the action's handler:

    var isCamActionEnabled = false // declare globally, if you need to set this from outside the current scope
    let picture = UIContextualAction(style: .normal, title:  " 📷 ", handler: { (ac: UIContextualAction, view: UIView, success:(Bool) -> Void) in
        guard isCamActionEnabled else { return }
        // ...
    })