I want to add UIContextMenuInteraction to a UIView with some transparent parts. When user interact with this view, iOS shows this view with a black background.
Is it possible to make those parts transparent or change the color?
I want to add UIContextMenuInteraction to a UIView with some transparent parts. When user interact with this view, iOS shows this view with a black background.
Is it possible to make those parts transparent or change the color?
On
Simpler solution to change context menu background color
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, previewForHighlightingMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
let previewTarget = UIPreviewTarget(container: self, center: <#your-view>.center)
let previewParams = UIPreviewParameters()
previewParams.backgroundColor = .clear
return UITargetedPreview(view: <#your-view>, parameters: previewParams, target: previewTarget)
}
On
Like MAGiGO said, you can use previewForHighlightingContextMenuWithConfiguration and previewForDismissingContextMenuWithConfiguration delegate methods to create your own UITargetedPreview and set its background color to clear.
func collectionView(_ collectionView: UICollectionView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview?
{
guard let indexPath = configuration.identifier as? IndexPath,
let cell = collectionView.cellForItem(at: indexPath)
else
{
return nil
}
let targetedPreview = UITargetedPreview(view: cell)
targetedPreview.parameters.backgroundColor = .clear
return targetedPreview
}
func collectionView(_ collectionView: UICollectionView, previewForDismissingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview?
{
guard let indexPath = configuration.identifier as? IndexPath,
let cell = collectionView.cellForItem(at: indexPath)
else
{
return nil
}
let targetedPreview = UITargetedPreview(view: cell)
targetedPreview.parameters.backgroundColor = .clear
return targetedPreview
}
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration?
{
return UIContextMenuConfiguration(identifier: NSIndexPath(item: indexPath.item, section: indexPath.section), previewProvider: nil) { elements -> UIMenu? in
....
Note: we are passing NSIndexPath as the identifier as it requires to conform to NSCopying protocol.
Finally I found the solution! You need to implement this function if you want to show a custom curved:
Here we are drawing a UIBezierPath around our custom shaped view. To do this you may need this extension too:
And this: