Force Touch TableView Cell Rounded Peek and Pop - iOS Swift

925 Views Asked by At

While peek and popping in a tableView, how do I make my previewingContext.sourceRect rounded?

At the moment:

  1. Force Touch on TableView Cell

  2. Set the previewing delegate as such:

    previewingContext.sourecRect = tableView.rectForRow(at: indexPath)

I have already tried setting the cell's cell.layer.cornerRadius & cell.layer.maskToBounds = true in cellForRowAtIndexPath, However the previewing soruce rect is still a perfect rectangle.

Relevant code:

UIVIewControllerPreviewingDelegate

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    if tableView.isEditing == false {
        guard let indexPath = tableView.indexPathForRow(at: location) else {
            print("Nil Cell Found: \(location)")
            return nil }
        guard let detailVC = storyboard?.instantiateViewController(withIdentifier: "view") as? ViewController else { return nil }


        previewingContext.sourceRect = tableView.rectForRow(at: indexPath)


        return detailVC

    } else {
        return nil

    }
}

TableView CellForRowAtIndexPath

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CustomCell else { fatalError("No cell with identifier: cell") }

            cell.titleLabel.text = "temp"

//            cell.inputView?.layer.cornerRadius = 16.0
//            cell.inputView?.layer.masksToBounds = true

//            cell.contentView.layer.cornerRadius = 16.0
//            cell.contentView.layer.masksToBounds = true

            cell.layer.cornerRadius = 16.0
            cell.layer.borderWidth = 2
            cell.layer.borderColor = UIColor.orange.cgColor
            cell.layer.masksToBounds = true

            return cell

        }

    }

Currently when just force touching on a tableview cell the blue box in the image below is selected and is the sourcerect, however I want the orange rounded rect (of one cell) to be the source rect.

enter image description here

1

There are 1 best solutions below

1
On

Do you want your Peek to be circle then Pop as a normal ViewController? Because sourceRect is the source of your Preview (in this case your cell) and not your actual Preview.

Try this if you want your Peek to become a circle, and Pop normally:

On your previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? add this before you return

detailVC.view.layer.cornerRadius = detailVC.view.bounds.width / 2
detailVC.view.layer.masksToBounds = true

And on your previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) add this before you show

detailVC.view.layer.cornerRadius = 0
show(detailVC, sender: self)