ios - 裁剪为形状的 SwiftUI 图像在上下文菜单中具有透明填充

标签 ios swift swiftui ios14

在我的 SwiftUI 应用程序中,我的 Assets 目录中有一张宽高比为 1:1 的图像。在我的代码中,我有一个 Image具有不同纵横比的 View ,将图像剪辑为新大小:

Image("My Image")
    .resizable()
    .aspectRatio(contentMode: .fill)
    .frame(width: 300, height: 250)
    .clipped()
image clipped to aspect ratio
但是当我将上下文菜单附加到此图像时(使用 contextMenu 修饰符),原始纵横比仍然存在,但具有透明填充:
image clipped to aspect ratio with context menu
如何将图像剪辑到上下文菜单内的新框架中,因此没有填充?

最佳答案

在 iOS 15 上,请参阅 accepted post .此解决方案适用于 iOS 14。

我可以通过添加 .contentShape(Rectangle()) 来解决这个问题图像修饰符:

Image("My Image")
    .resizable()
    .aspectRatio(contentMode: .fill)
    .frame(width: 300, height: 250)
    .clipped()
    .contentShape(Rectangle())
    .contextMenu {
        Text("Menu Item")
    }
image clipped to aspect ratio with context menu - correct behavior

关于ios - 裁剪为形状的 SwiftUI 图像在上下文菜单中具有透明填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62687224/

相关文章:

ios - 将字符串转换为 CLLocation 坐标

ios - 直接返回bool语句VS使用if返回bool

swift - 使用 SwiftUI 基于用户输入的动态文本字段

ios - NSURLSessionDownloadTaskDelegate JSON 响应

iphone - NSCoding 和 ARC

ios - 给图像添加参数;上传API如 "userid"= "50"

ios - Swift 3 录制音频并上传到 Firebase 存储和播放

ios - 引用 Storyboard中的后退按钮

ios - SwiftUI 简洁字体

Swift 联合发布两次