【问题标题】:Image in trailingSwipeActionsConfigurationForRowAt doesn't seem to worktrailingSwipeActionsConfigurationForRowAt 中的图像似乎不起作用
【发布时间】:2017-11-20 16:51:17
【问题描述】:

我正在尝试实现一个新的 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 渲染模式。无论哪种情况,图像都正确显示在调试器中:

但未能显示在模拟器中(“这里没有”标记我希望图像显示的位置):

我做错了吗?

【问题讨论】:

  • 您可以尝试删除图像上的红色背景吗?

标签: ios uiswipeactionsconfiguration


【解决方案1】:

老问题,

但对于那些仍在尝试解决此问题的人,请确保您的图像大小与您的单元格大小相同或更小

就我而言,我的图片太大,只显示白色背景

【讨论】:

    【解决方案2】:

    我也有同样的问题,几天来一直在尝试解决这个问题。尚未在任何地方找到解决方案。

    一个已弃用的解决方法是将 backgroundColor 设置为 patternImage ,如下所示:

    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!)
    

    【讨论】:

      【解决方案3】:

      发生这种情况是因为使用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])
              }
      

      图片示例:

      【讨论】:

      【解决方案4】:

      这在 iOS 13.4、Swift 4.2、Xcode 11.4 中对我有用

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

      这条线导致了这个——它可以在暗模式和亮模式下工作。

      在照片中:左侧是我的单元格右侧,右侧是红色垃圾桶按钮

      【讨论】:

        猜你喜欢
        • 2011-09-08
        • 2016-04-25
        • 1970-01-01
        • 1970-01-01
        • 2011-02-09
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 2016-11-29
        相关资源
        最近更新 更多