swiftuitableviewresizexcode8uitableviewrowaction

How to resize a UITableViewRowAction to fit custom sized tableViewCell? - Swift 3


In my app I customized my TableViewCell to display like a card. I also implemented custom UITableViewRowActions for those cells. However the actions look weird because I added a layer to make the cell look like a floating card.

Here's what it looks like And here's my code for my cellForRowAt function.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "someCell", for: indexPath) as! CustomCell
        cell.delegate = self

        cell.backgroundColor = .clear
        cell.contentView.backgroundColor = .clear

        let whiteRoundedView: UIView = UIView(frame: CGRect(x: 10, y: 8, width: self.view.frame.size.width - 20, height: cell.frame.size.height - 20))
        whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
        whiteRoundedView.layer.masksToBounds = false
        whiteRoundedView.layer.cornerRadius = 2.0
        whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
        whiteRoundedView.layer.shadowOpacity = 0.2
        cell.contentView.addSubview(whiteRoundedView)
        cell.contentView.sendSubview(toBack: whiteRoundedView)

        cell.updateCell()
        return cell
    }

And my UITableViewRowAction function. Forewarning: I am using this SwipeCellKit repository

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        if orientation == .left {
            guard isSwipeRightEnabled else { return nil }
            let completeAction = SwipeAction(style: .default, title: "Complete") { (action, index) in
                print("complete!")

            }
            completeAction.backgroundColor = .green
            return [completeAction]
        } else {
            let deleteAction = SwipeAction(style: .destructive, title: "Delete") { (action, index) in
                print("delete")
            }
            return [deleteAction]
        }
    }

Is there any way to resize the UITableViewRowAction to fit that mold of the cell?


Solution

  • It's not currently possible without making some changes to the framework.

    I do have plans to add some methods to SwipeTableViewCellDelegate which would expose each UIButton just before being displayed, and as swiping continues to occur. I imagine that would give you enough flexibility to achieve your layout.

    This shouldn't be too hard to do on your own fork in the meantime. Specifically, look at SwipeTableViewCell, inside the configureActionView method. It creates the SwipeActionsView which has a buttons property containing the swipe UIButton subclass. You just need to expose those to your app.