简体   繁体   English

trailingSwipeActionsConfigurationForRowAt 中的图像似乎不起作用

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

I'm trying to implement a new iOS11 feature that allows you to officially use an image in a tableview swipe action.我正在尝试实现一项新的 iOS11 功能,该功能允许您在 tableview 滑动操作中正式使用图像。

so, I came up with this:所以,我想出了这个:

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

I have this .png sitting in my assets catalog.我的资产目录中有这个 .png 文件。 I tried WITH and WITHOUt the rendering mode.我尝试了 WITH 和 WITHOUT 渲染模式。 In either case, the image is correctly shown in the debugger:在任何一种情况下,图像都在调试器中正确显示: 在此处输入图片说明

but fails to show up in the Simulator ("Nothing here" marks the place where I would expect the image to show up):但未能出现在模拟器中(“这里什么都没有”标志着我希望图像出现的地方): 在此处输入图片说明

Am I doing something wrong?我做错了什么吗?

Old question,老问题,

But for those who are still trying to solve this issue, make sure your image size is the same or smaller than your cell size.但是对于那些仍在尝试解决此问题的人,请确保您的图像大小等于或小于您的单元格大小。

For my case, my image is too huge and only shows white background就我而言,我的图像太大,只显示白色背景

I have the same exact issue, been trying to solve this for days now.我有同样的问题,几天来一直试图解决这个问题。 No solutions found yet anywhere.尚未在任何地方找到解决方案。

One deprecated workaround though is to set the backgroundColor constructed as patternImage like this:不过,一种已弃用的解决方法是将构造为 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!)

This happens because icons that set with myAction.image turn white by default, and .withRenderingMode(.alwaysOriginal) doesn't solve the issue.发生这种情况是因为使用myAction.image设置的图标默认变为白色,而.withRenderingMode(.alwaysOriginal)不能解决问题。 I'm fighting the same thing at the very this moment - rendering mode in that case doesn't seem to work at all.此刻我正在与同样的事情作斗争 - 在这种情况下渲染模式似乎根本不起作用。

I found only one way around it - use myAction.backgroundColor = UIColor(patternImage: theImage) instead of myAction.image .我发现只有一种解决方法 - 使用myAction.backgroundColor = UIColor(patternImage: theImage)而不是myAction.image

But if you will use this trick - please, notice that you will need to create an image with exact height as cell's height and with extended colour field on the right side of the image.但是,如果您将使用此技巧 - 请注意,您需要创建一个高度与单元格高度相同且在图像右侧具有扩展色域的图像。 It will help to keep image from repeating in visible on the screen area.这将有助于防止图像在屏幕区域可见重复。 And also, the text title will appear, so if you don't need it, just put an empty string in it.而且,文本标题会出现,所以如果你不需要它,只需在其中输入一个空字符串。

Code example:代码示例:

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])
        }

Image example:图片示例:

图片示例

This is what worked for me in iOS 13.4, Swift 4.2, Xcode 11.4这在 iOS 13.4、Swift 4.2、Xcode 11.4 中对我有用

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

That line resulted in this --and it works in dark mode and light mode.这条线导致了这一点——它可以在暗模式和亮模式下工作。

In the photo: right side of my cell on the left and the red trash button on the right在照片中:左侧我的手机右侧和右侧的红色垃圾箱按钮

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM