Customizing UIContextualAction

Hi


When implementing the swipe function for Table View Cells we use UIContextualAction to customize the buttons that shows under, such as normal or dusctructive, haw I can custoize them such as changing the color, font size, maybe showing icons ? and haw to link them to actions ?

--

Kindest Regards

Answered by Claude31 in 413372022

You can adapt the background color, add an image


    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
     
        let taken = UIContextualAction(style: .normal, title: "Taken") { (action, view, completion) in
            print("Just Swiped Taken", action)
            completion(true)
        }
        taken.backgroundColor =  UIColor(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
     
        let added = UIContextualAction(style: .normal, title: "Added") { (action, view, completion) in
            print("Just Swiped Added", action)
            completion(false)
        }
        added.image = UIImage(named: "Small Image")
        added.backgroundColor =  UIColor(red: 0.2436070212, green: 0.5393256153, blue: 0.1766586084, alpha: 1)
     
        let config = UISwipeActionsConfiguration(actions: [taken, added])
        config.performsFirstActionWithFullSwipe = false
     
        return config
    }

you cannot get an attributedString as title. A trick is to build it in the image you insert: https://stackoverflow.com/questions/26887563/how-to-change-uitableviewrowaction-title-color


May also read this old thread: https://forums.developer.apple.com/thread/113895

Accepted Answer

You can adapt the background color, add an image


    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
     
        let taken = UIContextualAction(style: .normal, title: "Taken") { (action, view, completion) in
            print("Just Swiped Taken", action)
            completion(true)
        }
        taken.backgroundColor =  UIColor(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)
     
        let added = UIContextualAction(style: .normal, title: "Added") { (action, view, completion) in
            print("Just Swiped Added", action)
            completion(false)
        }
        added.image = UIImage(named: "Small Image")
        added.backgroundColor =  UIColor(red: 0.2436070212, green: 0.5393256153, blue: 0.1766586084, alpha: 1)
     
        let config = UISwipeActionsConfiguration(actions: [taken, added])
        config.performsFirstActionWithFullSwipe = false
     
        return config
    }

you cannot get an attributedString as title. A trick is to build it in the image you insert: https://stackoverflow.com/questions/26887563/how-to-change-uitableviewrowaction-title-color


May also read this old thread: https://forums.developer.apple.com/thread/113895

Thats great, thanks allot

Customizing UIContextualAction
 
 
Q