gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:54:57 26 4
gpt4 key购买 nike

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


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

最佳答案

您可以为您的上下文菜单提供您自己的 previewProvider。只需创建一个带有 ImageView 的自定义 View Controller ,即可按所需大小预览图像:

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

编辑/更新:

没有任何 Action

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
UIContextMenuConfiguration(identifier: nil, previewProvider: {
ImagePreviewController(image: self.immy.image!)
})
}

关于swift - 在 ContextMenu 中更改 UIImage 的弹出窗口大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62925459/

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