微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS 12 UIContextualAction 图像不推断图像颜色

如何解决iOS 12 UIContextualAction 图像不推断图像颜色

在 iOS 12 中 UIContextualAction 不会推断图片的颜色。虽然它适用于 iOS 13 及更多。 我已经尝试了图标的所有渲染模式,但仍然无法正常工作。

    func tableView(_ tableView: UITableView,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .normal,title: "") { _,_,complete in
    }
    let editaction = UIContextualAction(style: .normal,complete in
    }
    
    deleteAction.image = UIImage(named: "trashcan")
    editaction.image = UIImage(named: "cellEdit")
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction,editaction])
    return configuration
}

enter image description here

enter image description here

解决方法

你可以试试这个

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

func tableView(_ tableView: UITableView,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    
    let deleteAction = UIContextualAction(style: .normal,title: "") { _,_,complete in
    }
    let editAction = UIContextualAction(style: .normal,complete in
    }
    
    
    if let cgImageTrashcan =  UIImage(named: "trashcan")?.cgImage {
        deleteAction.image = ImageWithoutRender(cgImage: cgImageTrashcan,scale: UIScreen.main.nativeScale,orientation: .up)
    }
    
    if let cgImageCellEdit =  UIImage(named: "cellEdit")?.cgImage {
        editAction.image = ImageWithoutRender(cgImage: cgImageCellEdit,orientation: .up)
    }
    
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction,editAction])
    return configuration
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。