Search code examples
iosswiftuitableviewstate-restoration3dtouch

Issues with 3D Touch and state restoration


I am working on implementing state restoration into my app, however I have ran into an issue. I recently added 3D Touch support to the table view, and it worked perfectly every time except now that I have added state restoration. When the app restores the table view, applying pressure to the cells does nothing, as if the table view hasn't been registered for previewing.

I have this code in viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    if #available(iOS 9.0, *) {
        if traitCollection.forceTouchCapability == UIForceTouchCapability.Available {
            registerForPreviewingWithDelegate(self, sourceView: view)
        }
    }
}

I tried moving it to viewDidAppear to see if it would help, but it didn't make a difference.

Any ideas?


Solution

  • Ok, I figured it out eventually. I had to move registerForPreviewingWithDelegate to viewWillAppear and I also had to add self. to the beginning.