Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - UITableView editActionsForRowAtIndexPath open UIPresentationController when click Edit

Hi is there any way to open an UIPresentationController when swipe left is triggered and it's click Edit ?

    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        let delete = ....
        let edit = UITableViewRowAction(style: .Normal, title: "Edit") { action, index in
            //OPEN UIPresentationController HERE
        }
        return [delete, edit]
}
like image 270
Bogdan Bogdanov Avatar asked Sep 06 '15 12:09

Bogdan Bogdanov


1 Answers

The same as @patchdiaz, I am not 100% sure what you would like to do. However this code block may be enough to be customized to achieve your goal:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let edit = UITableViewRowAction(style: .Normal, title: "Edit") { action, index in
        // OPEN UIPresentationController HERE
        let vc = UIViewController(nibName: nil, bundle: nil)
        vc.view.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
        vc.view.backgroundColor = UIColor.orangeColor()
        vc.modalPresentationStyle = .Popover

        let popover = vc.popoverPresentationController!
        let cell = tableView.cellForRowAtIndexPath(indexPath)!

        var cellAbsolutePosition = cell.superview!.convertPoint(cell.frame.origin, toView: nil)
        cellAbsolutePosition.x = cell.frame.width - 60
        popover.sourceRect = CGRect(origin: cellAbsolutePosition, size: cell.frame.size)
        popover.sourceView = tableView

        self.presentViewController(vc, animated: true, completion: nil)
    }
    return [edit]
}

It will show a popover right at the 'Edit' button position like this:

enter image description here

like image 54
Ducky Avatar answered Oct 21 '22 17:10

Ducky