1

假设您有一个长按时弹出的图像的上下文菜单。如何使弹出窗口更大,但保持相同的尺寸?


ViewControllerTableViewCell: UITableViewCell, UIContextMenuInteractionDelegate {

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    UIContextMenuConfiguration(identifier: nil, previewProvider: nil)  { _ in
        let share = UIAction(title: "", image: UIImage(systemName: "")) { _ in
            // share code
        }
        return UIMenu(title: "", children: [share])
    }
}

override func awakeFromNib() {
    super.awakeFromNib()
    immy.isUserInteractionEnabled = true
    immy.addInteraction(UIContextMenuInteraction(delegate: self))
}
4

1 回答 1

2

您可以将自己的 previewProvider 提供给上下文菜单。只需创建一个带有图像视图的自定义视图控制器,以便以所需大小预览图像:

import UIKit

class ImagePreviewController: UIViewController {
    private let imageView = UIImageView()
    init(image: UIImage) {
        super.init(nibName: nil, bundle: nil)
        preferredContentSize = image.size
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.image = image
        view = imageView
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}

然后只需将自定义预览提供程序实现添加到UIContextMenuConfiguration初始化程序:

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    UIContextMenuConfiguration(identifier: nil) {
        ImagePreviewController(image: self.immy.image!)
    } actionProvider: { _ in
        let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { _ in
           // share code
        }
        return UIMenu(title: "Profile Picture Menu", children: [share])
    }        
}

编辑/更新:

无需任何动作

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    UIContextMenuConfiguration(identifier: nil, previewProvider:  {
        ImagePreviewController(image: self.immy.image!)
    })
}
于 2020-07-16T03:49:05.683 回答