Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 正在尝试在tableView:EditActionsErrorwat中设置具有图像的按钮:_Ios_Swift_Uitableview_Uitableviewrowaction - Fatal编程技术网

Ios 正在尝试在tableView:EditActionsErrorwat中设置具有图像的按钮:

Ios 正在尝试在tableView:EditActionsErrorwat中设置具有图像的按钮:,ios,swift,uitableview,uitableviewrowaction,Ios,Swift,Uitableview,Uitableviewrowaction,使用Xcode 9.2,在UITableView中,我设置了一个按钮,当单元格被左扫时,按钮上有图像,没有文本。 以下是我使用的精确代码: func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let myButton = UITableViewRowAction(style: .no

使用Xcode 9.2,在UITableView中,我设置了一个按钮,当单元格被左扫时,按钮上有图像,没有文本。 以下是我使用的精确代码:

func tableView(_ tableView: UITableView,
               editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let myButton = UITableViewRowAction(style: .normal, title: "") {
        action, index in
        print("myButton was tapped")
    }
    myButton.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)

    return [myButton]
}
它可以工作,但图像显示在单元格底部

我怎样才能把它带到中心?

以防万一,下面是我用于测试的图像(70x70像素):

下面是它在表视图中的外观:


这是一种方法。您必须使用
UIGraphicsBeginImageContextWithOptions
创建图像

func swipeCellButtons() -> UIImage 
 {
     let commonWid : CGFloat = 40
     let commonHei : CGFloat = 70 // EACH ROW HEIGHT

     var img: UIImage = UIImage(named: "pause") // TRY IMAGE COLOR WHITE

     UIGraphicsBeginImageContextWithOptions(CGSize(width: commonWid, height: commonHei), false, UIScreen.main.scale)
     let context = UIGraphicsGetCurrentContext()
     context!.setFillColor(UIColor.clear.cgColor)
     context!.fill(CGRect(x: 0, y: 0, width: commonWid, height: commonHei))
     var img: UIImage = UIImage(named: "pause")!
     img.draw(in: CGRect(x: 5, y: 15, width: 30, height: 30))
     let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
     UIGraphicsEndImageContext()

     return newImage
 }


func tableView(_ tableView: UITableView,
               editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let myButton = UITableViewRowAction(style: .normal, title: "") {
        action, index in
        print("myButton was tapped")
    }

    let patternImg = swipeCellButtons()
    myButton.backgroundColor = UIColor(patternImage: patternImg)

    return [myButton]
}

这是一种方式。您必须使用
UIGraphicsBeginImageContextWithOptions
创建图像

func swipeCellButtons() -> UIImage 
 {
     let commonWid : CGFloat = 40
     let commonHei : CGFloat = 70 // EACH ROW HEIGHT

     var img: UIImage = UIImage(named: "pause") // TRY IMAGE COLOR WHITE

     UIGraphicsBeginImageContextWithOptions(CGSize(width: commonWid, height: commonHei), false, UIScreen.main.scale)
     let context = UIGraphicsGetCurrentContext()
     context!.setFillColor(UIColor.clear.cgColor)
     context!.fill(CGRect(x: 0, y: 0, width: commonWid, height: commonHei))
     var img: UIImage = UIImage(named: "pause")!
     img.draw(in: CGRect(x: 5, y: 15, width: 30, height: 30))
     let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
     UIGraphicsEndImageContext()

     return newImage
 }


func tableView(_ tableView: UITableView,
               editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let myButton = UITableViewRowAction(style: .normal, title: "") {
        action, index in
        print("myButton was tapped")
    }

    let patternImg = swipeCellButtons()
    myButton.backgroundColor = UIColor(patternImage: patternImg)

    return [myButton]
}

这很奇怪,对我来说确实是个中心。你能上传一张图片并展示它对你的外观以及你使用的图片的大小吗?我刚刚编辑了这篇文章,把我正在使用的测试图片包括进来。这很奇怪,它确实适合我。你能上传一张图片并展示它对你的外观以及你使用的图片的大小吗?我刚刚编辑了这篇文章,加入了我正在使用的测试图片。你的回答非常有用,让我有了明确的进展。不过我还没有完全做完。我能够按照您答案中的建议创建一个方便的函数,我称之为swipeCellImage,但我仍然有一些问题。我在这里重新提出了我的(新)问题:你的回答非常有帮助,让我取得了明显的进展。不过我还没有完全做完。我能够按照您答案中的建议创建一个方便的函数,我称之为swipeCellImage,但我仍然有一些问题。我在这里重新提出了我的(新)问题: