【问题标题】:Change the background color of a Context Menu using Swift使用 Swift 更改上下文菜单的背景颜色
【发布时间】:2020-09-06 06:44:56
【问题描述】:

我正在 UICollectionViewCell 上实现 UIContextMenu。在 UICollectionViewCell 内有一个半径为 15 的按钮。当我点击 UICollectionViewCell 时,这就是结果 。我想要的只是没有白色背景的按钮。我已经尝试过UICollectionViewCell.backgroundColor = .clear 并且似乎无法正常工作。如果有人可以帮助我,谢谢!

【问题讨论】:

    标签: ios swift swift3


    【解决方案1】:

    在您的 collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? 函数中添加 let identifier = "\(indexPath.row)" as NSString

    将该标识符放在UIContextMenuConfiguration(identifier: 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)
        } 
    

    【讨论】:

    • 你可以将索引路径作为UIContextMenuConfiguration的标识符传递给indexPath as NSCopying,并且不需要将行值转换为String并返回。
    【解决方案2】:

    您有UIPreviewParameters,您可以在其中轻松更改backgroundColor

    【讨论】:

      【解决方案3】:

      我认为在您的UICollectionViewCell 中,拨打contentView.backgroundColor = .clear 会起作用。

      您能提供更多信息吗?因为我的代码有效。

      【讨论】:

      • 你实现了上下文菜单吗?长按了吗?
      猜你喜欢
      • 2023-01-05
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2012-12-08
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      相关资源
      最近更新 更多