簡體   English   中英

trailingSwipeActionsConfigurationForRowAt 中的圖像似乎不起作用

[英]Image in trailingSwipeActionsConfigurationForRowAt doesn't seem to work

我正在嘗試實現一項新的 iOS11 功能,該功能允許您在 tableview 滑動操作中正式使用圖像。

所以,我想出了這個:

@available(iOS 11.0, *)
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .normal, title:  "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
            //whatever
            success(true)
        })
        let theImage: UIImage? = UIImage(named:"Delete")?.withRenderingMode(.alwaysOriginal)

        deleteAction.image = theImage
        return UISwipeActionsConfiguration(actions: [deleteAction])
    }

我的資產目錄中有這個 .png 文件。 我嘗試了 WITH 和 WITHOUT 渲染模式。 在任何一種情況下,圖像都在調試器中正確顯示: 在此處輸入圖片說明

但未能出現在模擬器中(“這里什么都沒有”標志着我希望圖像出現的地方): 在此處輸入圖片說明

我做錯了什么嗎?

老問題,

但是對於那些仍在嘗試解決此問題的人,請確保您的圖像大小等於或小於您的單元格大小。

就我而言,我的圖像太大,只顯示白色背景

我有同樣的問題,幾天來一直試圖解決這個問題。 尚未在任何地方找到解決方案。

不過,一種已棄用的解決方法是將構造為 patternImage 的 backgroundColor 設置為如下所示:

let deleteAction = UIContextualAction(style: .normal, title:  "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        //whatever
        success(true)
    })
let theImage: UIImage? = UIImage(named:"Delete")
deleteAction.backgroundColor = UIColor(patternImage: theImage!)

發生這種情況是因為使用myAction.image設置的圖標默認變為白色,而.withRenderingMode(.alwaysOriginal)不能解決問題。 此刻我正在與同樣的事情作斗爭 - 在這種情況下渲染模式似乎根本不起作用。

我發現只有一種解決方法 - 使用myAction.backgroundColor = UIColor(patternImage: theImage)而不是myAction.image

但是,如果您將使用此技巧 - 請注意,您需要創建一個高度與單元格高度相同且在圖像右側具有擴展色域的圖像。 這將有助於防止圖像在屏幕區域可見重復。 而且,文本標題會出現,所以如果你不需要它,只需在其中輸入一個空字符串。

代碼示例:

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

            let action = UIContextualAction(style: .normal, title: "", handler: { _, _, completionHandler in
                completionHandler(true)
            })

            let theImage: UIImage? = UIImage(named:"MyImage")
            action.backgroundColor = UIColor(patternImage: theImage!)

            return UISwipeActionsConfiguration(actions: [action])
        }

圖片示例:

圖片示例

這在 iOS 13.4、Swift 4.2、Xcode 11.4 中對我有用

let deleteActionImage = UIImage(systemName: "trash.fill")?.withTintColor(UIColor.systemRed, renderingMode: .alwaysOriginal)

這條線導致了這一點——它可以在暗模式和亮模式下工作。

在照片中:左側我的手機右側和右側的紅色垃圾箱按鈕

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM