![](/img/trans.png)
[英]Dynamic CollectionView cell in a normal CollectionView cell
[英]Manually invoke context menu in collectionView for cell
我使用如下委托方法實現了上下文菜單的方法:
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
configureContextMenu(index: indexPath.row)
}
func configureContextMenu(index: Int) -> UIContextMenuConfiguration {
let context = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { (action) -> UIMenu? in
let edit = UIAction(title: "Edit", image: UIImage(systemName: "square.and.pencil"), identifier: nil, discoverabilityTitle: nil, state: .off) { (_) in
print("edit button clicked")
}
let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash"), identifier: nil, discoverabilityTitle: nil,attributes: .destructive, state: .off) { (_) in
print("delete button clicked")
}
return UIMenu(title: "Options", image: nil, identifier: nil, options: UIMenu.Options.displayInline, children: [edit,delete])
}
return context
}
它工作正常,如我所願。 但我的目標受眾是年長的觀眾,我不確定他們是否知道他們可以為上下文菜單保存單元格。 所以我想在右上角添加三個點,然后在他們點擊后顯示相同的單元格上下文菜單。 有可能做到這一點嗎? 如何手動調用它?
感謝幫助
我相信手動調用 UIContextMenus 是不可能的,因為它們的交互觸發器和事件管理是按照文檔在內部處理的
上下文菜單交互 object 跟蹤支持 3D Touch 的設備上的Force Touch手勢,以及不支持它的設備上的長按手勢。
我能想到的唯一解決方法是在您的集合視圖單元格上使用帶有 UIButton 的 UIMenu。
我將其稱為解決方案而不是解決方案,因為您將獲得一鍵式用戶體驗,但您將失去 UIContextMenu 為您提供集合視圖和表格視圖的用戶交互的模糊背景和焦點。
盡管如此,這是我對自定義 UICollectionViewCell 的實現,其中按鈕固定在單元格的邊緣,但是您可以根據需要調整其大小。 您可以檢查這對您的用例是否可行:
class ButtonCollectionViewCell: UICollectionViewCell
{
static let reuseIdentifier = "ButtonCollectionViewCell"
let titleLabel = UILabel()
let hiddenButton = UIButton()
override init(frame: CGRect)
{
super.init(frame: frame)
contentView.backgroundColor = .yellow
configureLabel()
configureButton()
layoutIfNeeded()
}
required init?(coder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
private func configureLabel()
{
contentView.addSubview(titleLabel)
// Auto layout config to pin label to the edges of the content view
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
private func configureButton()
{
addSubview(hiddenButton)
// I add this red color so you can see the button takes up the whole cell
hiddenButton.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.75)
// Auto layout config to pin button to the edges of the content view
hiddenButton.translatesAutoresizingMaskIntoConstraints = false
hiddenButton.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
hiddenButton.topAnchor.constraint(equalTo: topAnchor).isActive = true
hiddenButton.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
hiddenButton.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
// Configure menu
hiddenButton.showsMenuAsPrimaryAction = true
hiddenButton.menu = UIMenu(title: "Select an option", children: [
UIAction(title: "Option 1") { action in
// do your work
},
UIAction(title: "Option 2") { action in
// do your work
},
])
}
}
結果如下:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.