用戶。我有一個我似乎無法弄清楚的問題。當我在 UICollectionView 中按下一行時,我想顯示一個 UIMenu,所以我添加了一個 UIButton,它在每個單元格中從邊緣到邊緣跨越。按下按鈕時,會出現一個 UIMenu。問題是 UICollectionView 不能在每個單元格中使用 UIButton 滾動。系統將 UIButton 優先于滾動。我希望只有一個 UILabel 和一個顯示何時呼叫“didSelectItemAt indexPath”的選單,但 UIMenus 僅適用于 UIButton 和 UINavigationBarButton。
如果有人對我有解決方案,請幫忙!非常感謝,親切的問候??
uj5u.com熱心網友回復:
我懷疑我可能錯了,您已將delaysContentTouchesUICollectionView 設定為 false。
這將使任何觸摸都被解釋為按鈕點擊。
這是我的設定,而不是從單元格中洗掉按鈕,這聽起來與您的相似,并給出了所需的最終結果:
帶有標簽和按鈕的自定義 UICollectionView 跨越整個集合視圖單元格
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
},
])
}
}
在視圖控制器中
class UICollectionViewButtonCellViewController: UIViewController
{
private var collectionView: UICollectionView!
override func viewDidLoad()
{
super.viewDidLoad()
title = "Button Cell Example"
view.backgroundColor = .white
configureCollectionView()
}
private func configureCollectionView()
{
collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: createLayout())
collectionView.register(ButtonCollectionViewCell.self,
forCellWithReuseIdentifier: ButtonCollectionViewCell.reuseIdentifier)
collectionView.dataSource = self
// do not have the below line of code, by default delaysContentTouches is true
// collectionView.delaysContentTouches = false
view.addSubview(collectionView)
// Auto layout config to pin collection view to the edges of the view
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
}
private func createLayout() -> UICollectionViewFlowLayout
{
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let availableWidth = view.frame.width
let interItemSpacing: CGFloat = 5
// (available width - inter item spacing) / 2
let itemWidth = (availableWidth - interItemSpacing) / 2
layout.itemSize = CGSize(width: itemWidth, height: 100)
layout.minimumInteritemSpacing = interItemSpacing
layout.minimumLineSpacing = 5
return layout
}
}
extension UICollectionViewButtonCellViewController: UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int
{
return 20
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ButtonCollectionViewCell.reuseIdentifier,
for: indexPath) as! ButtonCollectionViewCell
cell.titleLabel.text = "Tap cell \(indexPath.item)"
return cell
}
}
這為我提供了可滾動 UICollectionView 的所需輸出,它的點擊導致 UIMenu 互動。

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420329.html
標籤:
