上下文菜单预览提供程序以及情节提要选择

时间:2019-08-21 10:05:48

标签: ios uikit contextmenu ios13

在情节提要中,我有一个带有静态单元格的表视图控制器,每个单元都执行segue来呈现视图控制器。

到目前为止,可以在情节提要中启用Peek和Pop支持,并且将为此实现所有旧版API。

上下文菜单目前似乎没有这样的选项。

我的问题是,是否可以通过某种方式从按需序列中获取控制器,以便我可以由UIContextMenuContentPreviewProvider退还?其他任何建议(除了更改情节提要中的静态单元格)都欢迎!

1 个答案:

答案 0 :(得分:0)

解决了这个问题,但使用了私有API,但不应该使用它仅用于教育目的!

我为每个单元格和segue赋予了相同的标识符。然后使用私有API,实例化segue的目标控制器,并将其作为预览返回。

我们声明一些私有API:

@interface UIViewController ()

- (id)_segueTemplateWithIdentifier:(id)arg1;

@end

@interface NSObject ()

- (id)instantiateOrFindDestinationViewControllerWithSender:(id)arg1;

@end

然后在实现中将其绑在一起:

@implementation DemoGalleryController

- (nullable UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0))
{
    return [UIContextMenuConfiguration configurationWithIdentifier:@"Preview" previewProvider:^ UIViewController* {
        NSString* cellIdentifier = [tableView cellForRowAtIndexPath:indexPath].reuseIdentifier;
        id segueTemplate = [self _segueTemplateWithIdentifier:cellIdentifier];
        return [segueTemplate instantiateOrFindDestinationViewControllerWithSender:self];;
    } actionProvider:nil];
}

- (void)tableView:(UITableView *)tableView willPerformPreviewActionForMenuWithConfiguration:(UIContextMenuConfiguration *)configuration animator:(id<UIContextMenuInteractionCommitAnimating>)animator API_AVAILABLE(ios(13.0))
{
    UIViewController* vc = animator.previewViewController;

    [animator addCompletion:^{
        [self presentViewController:vc animated:YES completion:nil];
    }];
}

@end