【问题标题】:UIContextualAction not displaying image on iOS 12.4, and that code works for iOS 13.4 fineUIContextualAction 不在 iOS 12.4 上显示图像,该代码适用于 iOS 13.4
【发布时间】:2020-09-02 03:05:41
【问题描述】:

谁能解释一下如何解决这个问题?

这是它现在在 iPhone X (iOS 12.4) 上显示的内容

下面是我的代码

@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
}

【问题讨论】:

    标签: ios swift image uicontextualaction


    【解决方案1】:
        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
    }
    

    试试这个

    【讨论】:

      猜你喜欢
      • 2012-11-05
      • 2021-08-13
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多