首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改UIImage在ContextMenu中的弹出式大小?

更改UIImage在ContextMenu中的弹出式大小?
EN

Stack Overflow用户
提问于 2020-07-16 00:01:45
回答 1查看 1.2K关注 0票数 2

假设您有一个图像的上下文菜单,该菜单在长时间按下时会弹出。如何才能使弹出窗口变大,但保持相同的维度?

代码语言:javascript
运行
复制
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])
    }
}

代码语言:javascript
运行
复制
override func awakeFromNib() {
    super.awakeFromNib()
    immy.isUserInteractionEnabled = true
    immy.addInteraction(UIContextMenuInteraction(delegate: self))
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-16 03:49:05

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

代码语言:javascript
运行
复制
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初始化程序:

代码语言:javascript
运行
复制
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])
    }        
}

编辑/更新:

不采取任何行动

代码语言:javascript
运行
复制
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    UIContextMenuConfiguration(identifier: nil, previewProvider:  {
        ImagePreviewController(image: self.immy.image!)
    })
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62925459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档