更改ContextMenu中UIImage的弹出窗口大小?

时间:2020-07-16 00:01:45

标签: swift firebase uiimageview contextmenu uicontextmenuinteraction

说您有一个上下文菜单,该菜单长按会弹出。如何使弹出窗口更大,但保持相同的尺寸?


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))
}

1 个答案:

答案 0 :(得分:1)

您可以在上下文菜单中提供自己的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!)
    })
}