您需要使用contextMenuInteraction:previewForHighlightingMenuWithConfiguration: 方法。它允许为 ContextMenu 返回一个自定义预览,包括设置UIPreviewParameters,其中包括backgroundColor,应该设置为透明。
主要挑战是它需要一个视图才能显示,但在react-native-context-menu-view 中,我们在交互时只能访问UIContextMenuInteraction。
为了解决这个问题,我们可以创建一个interaction -> view 地图:
NSMapTable *interactionToViewMap = [NSMapTable weakToWeakObjectsMapTable];
然后,在insertReactSubview: 中的ContextMenuView.m 中,我们记得interaction-to-view 关联:
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
{
[super insertReactSubview:subview atIndex:atIndex];
if (@available(iOS 13.0, *)) {
UIContextMenuInteraction* contextInteraction = [[UIContextMenuInteraction alloc] initWithDelegate:self];
// Map interaction to its view, so that we can retrieve view by interaction later
[interactionToViewMap setObject:subview forKey:contextInteraction]; // <-- new line
[subview addInteraction:contextInteraction];
}
}
然后添加一个方法,该方法将使用地图显示具有透明背景的预览:
- (nullable UITargetedPreview *)contextMenuInteraction:(UIContextMenuInteraction *)interaction previewForHighlightingMenuWithConfiguration:(UIContextMenuConfiguration *)configuration API_AVAILABLE(ios(13.0)){
// Set background to transparent to avoid unnecessary highlighting.
UIPreviewParameters *params = [UIPreviewParameters alloc];
params.backgroundColor = [UIColor clearColor];
return [[UITargetedPreview alloc] initWithView:[interactionToViewMap objectForKey:interaction] parameters:params];
}