标题的UITableViewRowAction图像

use*_*462 30 uitableview ios swift xcode6 uitableviewrowaction

我做了一个自定义的UITableViewRowAction.现在我想添加一个图像而不是文本.我知道这是可能的,但不知道该怎么做.你们中的某些人是否知道如何在Swift中做到这一点并想帮助我?谢谢你的回答!

dim*_*iax 45

您需要将UIImage设置为行动的backgroundColor,具体来说是:

迅速:

extension ViewController: UITableViewDelegate {
  func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let askAction = UIContextualAction(style: .normal, title: nil) { action, view, complete in
      print("Ask!")
      complete(true)
    }

    // here set your image and background color
    askAction.image = IMAGE
    askAction.backgroundColor = .darkGray

    let blockAction = UIContextualAction(style: .destructive, title: "Block") { action, view, complete in
      print("Block")
      complete(true)
    }

    return UISwipeActionsConfiguration(actions: [blockAction, askAction])
  }

  func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.textLabel?.text = "row: \(indexPath.row)"
  }
}
Run Code Online (Sandbox Code Playgroud)

Objective-C的:

UIColor(patternImage: UIImage(named: "IMAGE_NAME"))
Run Code Online (Sandbox Code Playgroud)

  • 对于图像,我把CocoaPod放在一起,试图尽可能优雅地解决这个问题:https://github.com/benguild/BGTableViewRowActionWithImage (3认同)
  • @dimpiax:好的.这适用于图像.但是如何将背景颜色和图像放在一起. (3认同)
  • 使用图像时,必须确定按钮的大小并将其应用于图像.文本也必须是一定数量的"空格".如果在`rowActionWithStyle:title:handler:`中设置了标题文本,则图像和文本可能会重叠.标题文本中的空格数决定了图像的宽度. (2认同)

Pet*_*inz 10

Swift 4(iOS 11+):

iOS 11现在支持(仅)图像以显示在操作按钮中.您只需在表视图委托对象中初始化UISwipeActionsConfiguration对象:

extension MyTableViewController:UITableViewDelegate {

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

        let deleteAction = UIContextualAction(style: .normal, title:  nil, handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in

                debugPrint("Delete tapped")

                success(true)
            })

        deleteAction.image = UIImage(named: "icon_delete.png")
        deleteAction.backgroundColor = UIColor.red

        return UISwipeActionsConfiguration(actions: [deleteAction])
    }
Run Code Online (Sandbox Code Playgroud)

}


小智 7

我制作了这个简单的UITableViewRowAction类别,以便为我的动作设置图标.您可以设置图像,背景颜色,单元格高度(以管理动态单元格)和图标大小(以百分比表示).

extension UITableViewRowAction {

  func setIcon(iconImage: UIImage, backColor: UIColor, cellHeight: CGFloat, iconSizePercentage: CGFloat)
  {
    let iconHeight = cellHeight * iconSizePercentage
    let margin = (cellHeight - iconHeight) / 2 as CGFloat

    UIGraphicsBeginImageContextWithOptions(CGSize(width: cellHeight, height: cellHeight), false, 0)
    let context = UIGraphicsGetCurrentContext()

    backColor.setFill()
    context!.fill(CGRect(x:0, y:0, width:cellHeight, height:cellHeight))

    iconImage.draw(in: CGRect(x: margin, y: margin, width: iconHeight, height: iconHeight))

    let actionImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.backgroundColor = UIColor.init(patternImage: actionImage!)
  }
}
Run Code Online (Sandbox Code Playgroud)