gpt4 book ai didi

ios - 显示不带 UIButton 或 UINavigationButton 的 UIMenu

转载 作者:行者123 更新时间:2023-12-05 05:48:27 26 4
gpt4 key购买 nike

用户。我遇到了一个我似乎无法弄清楚的问题。我想在我的 UICollectionView 中按一行时显示一个 UIMenu,因此我添加了一个 UIButton,它在每个单元格中从边到边跨越。按下按钮时,会出现一个 UIMenu。问题是 UICollectionView 不能滚动,每个单元格中都有一个 UIButton。系统优先考虑 UIButton 而不是滚动。我希望只有一个 UILabel 和一个菜单显示何时调用“didSelectItemAt indexPath”,但 UIMenus 仅适用于 UIButton 和 UINavigationBarButton。

如果有人对我有解决方案,请帮忙!非常感谢你,亲切的问候😊

最佳答案

我怀疑而且我可能是错的,您已将 UICollectionView 的 delaysContentTouches 设置为 false。

这将使任何触摸都被解释为按钮点击。

这里不是从单元格中删除按钮,而是我的设置,它听起来与您的设置相似,并提供了所需的最终结果:

带有跨越整个 Collection View 单元格的标签和按钮的自定义 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
},
])
}
}

在 View Controller 中

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 交互。

UICollectionView with Custom UICollectionViewCell to show UIMenu UIContextMenuInteraction interaction triggered from a UIButton

关于ios - 显示不带 UIButton 或 UINavigationButton 的 UIMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70813955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com