UIContextualAction not displaying image on iOS 12.4, and that code works for iOS 13.4 fine

374 Views Asked by At

enter image description here

Can anyone explain how to solve that ?

Here is what it shows me now on iPhone X (iOS 12.4)

Here is my code below

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    guard let account = accounts?[indexPath.row] else { return nil }

    getAndSetAmountConstraint(from: account)

    let editAction = UIContextualAction(style: .normal, title: nil) { [weak self] (_, _, completionHandler) in
        self?.showEditAlertViewWith(account: account)
        completionHandler(true)
    }
    let deleteAction = UIContextualAction(style: .normal, title: nil) { [weak self] (_, _, completionHandler) in
        self?.removeRow(at: indexPath)
        completionHandler(true)
    }

    editAction.image = UIImage(named: "edit")
    deleteAction.image = UIImage(named: "delete")
    editAction.backgroundColor = .lightGray
    deleteAction.backgroundColor = .lightGray

    let actions = UISwipeActionsConfiguration(actions: [deleteAction, editAction])
    return actions
}
1

There are 1 best solutions below

0
Giorgi Sandroshvili On
    class ImageWithoutRender: UIImage {
    override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
        return self
    }
}

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    guard let account = accounts?[indexPath.row] else { return nil }

    getAndSetAmountConstraint(from: account)

    let editAction = UIContextualAction(style: .normal, title: nil) { [weak self] (_, _, completionHandler) in
        self?.showEditAlertViewWith(account: account)
        completionHandler(true)
    }
    let deleteAction = UIContextualAction(style: .normal, title: nil) { [weak self] (_, _, completionHandler) in
        self?.removeRow(at: indexPath)
        completionHandler(true)
    }

    if let cgImageEdit =  UIImage(named: "edit")?.cgImage {
        editAction.image = ImageWithoutRender(cgImage: cgImageEdit, scale: UIScreen.main.nativeScale, orientation: .up)
    }
    
    if let cgImageDelete =  UIImage(named: "delete")?.cgImage {
        deleteAction.image = ImageWithoutRender(cgImage: cgImageDelete, scale: UIScreen.main.nativeScale, orientation: .up)
    }
    editAction.backgroundColor = .lightGray
    deleteAction.backgroundColor = .lightGray

    let actions = UISwipeActionsConfiguration(actions: [deleteAction, editAction])
    return actions
}

TRY THIS