TablviewCell 内按钮的 UIMenu 未正确显示

问题描述 投票:0回答:1

我正在显示按钮菜单,如下所示:

final class YourCustomCollectionViewCell: UICollectionViewCell {

    private let menuButton: UIButton = {
        let button = UIButton()
        button.showsMenuAsPrimaryAction = true
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()
    
    override class func awakeFromNib() {
        super.awakeFromNib()
        
        self.contentView.addSubview(menuButton)
        configureButtonConstraints()
        configureMenu()
    }
    
    private func configureButtonConstraints() {
        NSLayoutConstraint.activate([
            menuButton.topAnchor.constraint(equalTo: self.topAnchor),
            menuButton.leadingAnchor.constraint(equalTo: self.leadingAnchor),
            menuButton.trailingAnchor.constraint(equalTo: self.trailingAnchor),
            menuButton.bottomAnchor.constraint(equalTo: self.bottomAnchor)
        ])
    }

    private func configureMenu() {
        let editAction = UIAction(title: "Edit") { _ in
            print("Edit Button Clicked")
        }
        
        let deleteAction = UIAction(title: "Delete") { _ in
            print("Delete Button Clicked")
        }
        
        let menuForCell = UIMenu(title: "Options", options: UIMenu.Options.displayInline, children: [editAction, deleteAction])
        menuButton.menu = menuForCell
    }
}

显示菜单正常,但显示1秒后 菜单已移至屏幕顶部,并且未显示在按钮旁边。

我该如何解决这个问题?

谢谢你,

ios swift uitableview contextmenu uimenucontroller
1个回答
0
投票

您没有提供有关这些情况的任何信息,但如果您不使用自定义 xib,那么您应该使用 init 而不是 awakefromnib。

class MyCollectionViewCell: UICollectionViewCell {
   
    
    private let menuButton: UIButton = {
        let button = UIButton()
        button.showsMenuAsPrimaryAction = true
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(menuButton)
        configureButtonConstraints()
        configureMenu()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configureButtonConstraints() {
        NSLayoutConstraint.activate([
            menuButton.topAnchor.constraint(equalTo: contentView.topAnchor),
            menuButton.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            menuButton.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            menuButton.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
        ])
    }

    private func configureMenu() {
        let editAction = UIAction(title: "Edit") { _ in
            print("Edit Button Clicked")
        }
        
        let deleteAction = UIAction(title: "Delete") { _ in
            print("Delete Button Clicked")
        }
        
        let menuForCell = UIMenu(title: "Options", options: UIMenu.Options.displayInline, children: [editAction, deleteAction])
        menuButton.menu = menuForCell
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.