首页 > 解决方案 > 上下文菜单选择颜色和框架大小 Swift 5 iOS 13

问题描述

我正在尝试通过新的 iOS 13 上下文菜单为我的应用程序实现一个简单的报告/阻止功能。它似乎工作正常,但是,我目前无法解决它的外观问题。

  1. 当我长按 aUICollectionViewCell时,它会在黑暗模式下突出显示黑色,这看起来很糟糕,因为我没有全黑色的背景颜色。如何将其更改为清晰的颜色或我想要的颜色?
  2. 我有一个显示用户消息的气泡,我想使用气泡的上下文菜单。问题是,当上下文菜单默认预览我的单元格时,它会裁剪气泡的底部,看起来只是整个消息的一部分。我尝试使用单元格的高度和其他一些参数 - 没有任何效果。

我该如何解决这些问题?请帮忙。

****我的代码是:****

@available(iOS 13.0, *)

override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {

    let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil){ action in

            let cell = collectionView.cellForItem(at: IndexPath.init(row: indexPath.row, section: 0)) as? ChatCell

        cell?.tintColor = .clear

        self.view.backgroundColor = .clear


           let messageText = cell?.textView.text

            let userNameZdes = cell?.nameView.text

            let report = UIAction(title: "Report", image: UIImage(systemName: "exclamationmark.bubble"), identifier: UIAction.Identifier(rawValue: "report")) {_ in
                print("report clicked..")

                if (cell?.textView.text.count)! > 2 {

                     print("the text of the message = \(messageText!) & the user is \(userNameZdes!)")

                } else {

                    print("the user has attached a bad pic")
                }

            }

            let block = UIAction(title: "Block user", image: UIImage(systemName: "person.crop.circle.badge.xmark"), identifier: UIAction.Identifier(rawValue: "block"), attributes: .destructive) {_ in
                print("block clicked..")
            }
            return UIMenu(title: "Message Actions", image: nil, identifier: nil, children: [report, block])
        }
        return configuration
}

标签: iosswiftiphoneuicollectionviewios13

解决方案


我今天遇到了同样的困难,我找到了一个对我有用的解决方案,所以我希望它也对你有用。

我在这篇文章中找到了解决方案:https ://kylebashour.com/posts/context-menu-guide

如果您向下滚动到该部分:UITargetedPreview,有一个示例,UITableView但是您可以为 UICollectionView 应用相同的解决方案:)


推荐阅读