Vidit Bhargava
Vidit Bhargava

Reputation: 31

Issue with SFSafariViewController on 3DTouch (Pop State)

I'm trying to implement the Peek and Pop functionality in a TableViewController, so that when I 3DTouch any of the cells, it shows me a preview of the SFSafariViewController they'll take me to on tapping them. The peek functionality works just fine, but as soon as it moves to the pop state, it doesn't give SFSafariViewController in the desired state but instead end up showing the navigation controller from the previous view, and a poorly sized view showing the SFViewController (Image Here).

This is how I've programmed the peek and pop functionality inside my TableViewController:

extension MyBlogsTableViewController: UIViewControllerPreviewingDelegate {

func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    if let indexPath = tableView.indexPathForRowAtPoint(location) {

            let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyBlogsTableViewCell

            let destinationViewController = SFViewControllerToExplore(indexPath.row)
            destinationViewController.preferredContentSize = CGSize(width: 0.0, height: 0.0)

            previewingContext.sourceRect = cell.frame

            return destinationViewController
    }
    return nil
}

func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
    navigationController?.pushViewController(viewControllerToCommit, animated: true)
}

private func touchedView(view: UIView, location: CGPoint) -> Bool {
    let locationInView = view.convertPoint(location, fromView: tableView)
    return CGRectContainsPoint(view.bounds, locationInView)
}

private func SFViewControllerToExplore(index: Int) -> UIViewController {
    var destinationController: SFSafariViewController

    destinationController = SFSafariViewController(URL: NSURL(string: identifierOfLinks[index])!, entersReaderIfAvailable: true)

    return destinationController
}

}

Upvotes: 1

Views: 225

Answers (1)

Vidit Bhargava
Vidit Bhargava

Reputation: 31

Figured it out. So in the following code :

func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
     navigationController?.pushViewController(viewControllerToCommit, animated: true)
}  

I shouldn't actually be using pushViewController, I should instead, be using presentViewController

Corrected Code:

func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
     navigationController?.presentViewController(viewControllerToCommit, animated: true, completion: nil)
}  

Upvotes: 1

Related Questions