Ios 使用Swift更改关联菜单的背景色

Ios 使用Swift更改关联菜单的背景色,ios,swift,swift3,Ios,Swift,Swift3,我正在UICollectionViewCell上实现UIContextMenu。UICollectionViewCell内有一个半径为15的按钮。当我点击UICollectionViewCell时,这就是结果 . 我想要的只是没有白色背景的按钮。我已经尝试了UICollectionViewCell.backgroundColor=.clear,但似乎不起作用。如果有人能帮我,谢谢 我认为在您的UICollectionViewCell中,调用contentView.backgroundColor=

我正在UICollectionViewCell上实现UIContextMenu。UICollectionViewCell内有一个半径为15的按钮。当我点击UICollectionViewCell时,这就是结果
. 我想要的只是没有白色背景的按钮。我已经尝试了
UICollectionViewCell.backgroundColor=.clear
,但似乎不起作用。如果有人能帮我,谢谢

我认为在您的
UICollectionViewCell
中,调用
contentView.backgroundColor=.clear
就可以了


你能提供进一步的信息吗?因为我的代码有效。

我认为在您的
UICollectionViewCell
中,调用
contentView.backgroundColor=.clear
会有效


你能提供进一步的信息吗?因为我的代码可以工作。

collectionView(\uCollectionView:UICollectionView,contextMenuConfigurationForItemAt indexPath:IndexXPath,point:CGPoint)->UIContextMenuConfiguration?
函数中添加
let identifier=“\(indexPath.row)”作为NSString

将该标识符放入
UIContextMenuConfiguration(标识符:identifier,previewProvider:previewProvider,actionProvider:actionProvider)
init

下一步添加此函数

func collectionView(_ collectionView: UICollectionView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
        guard
            let identifier = configuration.identifier as? String,
            let index = Int(identifier),
            let cell = collectionView.cellForItem(at: IndexPath(row: index, section: 0)) as? YourCell

              else {
                return nil
            }
        return UITargetedPreview(view: cell.YourButton)
    } 

let identifier=“\(indexPath.row)”添加为
collectionView(\uCollectionView:UICollectionView,ContextMenuConfiguration for Itemat indexPath:IndexXPath,point:CGPoint)->UIContextMenuConfiguration?
函数中的NSString

将该标识符放入
UIContextMenuConfiguration(标识符:identifier,previewProvider:previewProvider,actionProvider:actionProvider)
init

下一步添加此函数

func collectionView(_ collectionView: UICollectionView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
        guard
            let identifier = configuration.identifier as? String,
            let index = Int(identifier),
            let cell = collectionView.cellForItem(at: IndexPath(row: index, section: 0)) as? YourCell

              else {
                return nil
            }
        return UITargetedPreview(view: cell.YourButton)
    } 

您有
UIPreviewParameters
可以轻松更改
backgroundColor

您有
UIPreviewParameters
可以轻松更改
backgroundColor

您实现了上下文菜单吗?长时间按下它?你们实现上下文菜单了吗?您可以将索引路径作为
UIContextMenuConfiguration
doing
indexPath作为NSCopying
的标识符传递,您不需要将行值转换为字符串并返回。您可以将索引路径作为
UIContextMenuConfiguration
的标识符传递,将
indexPath作为NSCopying
,也不需要将行值转换为字符串并返回。