I am building a contact app in Xcode 9, Swift 4, iOS 11 but I am having a tough time attempting to implement Peek and Pop into my application. Here's how it's supposed to work: The user is presented with their list of contacts on a UITableView. I want the user to be able to use 3D Touch to peek at a contact without having to open the contact and pop it if they wish to open the contact. I found a great tutorial at Techotopia on using 3D Touch but haven't been able to get it to work. Xcode is not giving me any errors, yet when I debug the app on my iPhone 8, it does not work. Here is my code:
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { guard let indexPath = tableView.indexPathForRow(at: location), let cell = tableView.cellForRow(at: indexPath) else { return nil } guard let detailViewController = storyboard?.instantiateViewController( withIdentifier: "InfoContactController") as? InfoContactController else { return nil } detailViewController.preferredContentSize = CGSize(width: 0.0, height: 600) previewingContext.sourceRect = cell.frame return detailViewController } func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { show(viewControllerToCommit, sender: self) }
In the tutorial at Techotopia, they use this code in viewDidLoad
to register the previewing delegate:
if traitCollection.forceTouchCapability == .available { registerForPreviewing(with: self, sourceView: view) } else { print("3D Touch Not Available") }
The above code prints "3D Touch Not Available" in my console although I was obviously using a 3D Touch capable device. So, I did some research on Google and found this code which works:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) switch traitCollection.forceTouchCapability { case .available: print("Available") case .unavailable: print("Unavailable") case .unknown: print("Unknown") } }
This is my code. It gives me no errors in Xcode but when I 3D Touch in the app, nothing happens and I do not know why.
Any help would be appreciated!
0 comments:
Post a Comment