Search code examples
ios3dtouchpeek

iOS 3D Touch Peek/Pop without Storyboards


I am trying to get peek/pop to work on a non storyboard Objective-C project and I keep getting a white background instead of the blur background. I have tried on the simulator and on my iPhone.

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
if ([self.presentedViewController isKindOfClass:[LoadTreatmentDetailViewController class]]) {
    return nil;
}

NSIndexPath *indexPath = [self.loadTreatmentsTableView indexPathForRowAtPoint:location];

if(indexPath){
    AllTreatmentsTableViewCell *tableCell = [self.loadTreatmentsTableView cellForRowAtIndexPath:indexPath];

    if(tableCell.treatment){
        LoadTreatmentDetailViewController *previewController = [[LoadTreatmentDetailViewController alloc] initWithTreatment:tableCell.treatment withContext:self.context];
        if(previewController){
            previewController.preferredContentSize = CGSizeMake(0.0, 0.0);
            previewingContext.sourceRect = tableCell.frame;

            return previewController;
        }
    }
}
return nil;
}

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
[self presentViewController:viewControllerToCommit animated:YES completion:nil];
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
if([self isForceTouchAvailable]){
    if(!self.previewingContext){
        self.previewingContext = [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }
}else{
    if(self.previewingContext){
        [self unregisterForPreviewingWithContext:self.previewingContext];
        self.previewingContext = nil;
    }
}
}

- (BOOL)isForceTouchAvailable {
BOOL isForceTouchAvailable = NO;
if([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]){
    isForceTouchAvailable = self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
}
return isForceTouchAvailable;
}

Is it possible?

Thanks for any help!


Solution

  • It was the UIApperance I was using for the Scrollview.