我必須解決 SwiftUI 上的影像問題。我目前必須將收到的影像填充到某個垂直容器中,這些容器是 LazyVGrid 的單元格,但問題是可點擊區域超出了指定區域。
我使用了輔助功能檢查器,我發現多余的區域是由于影像。(附截圖)
我正在使用的代碼類似于以下內容:
Image(uiImage: image.image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(
maxWidth: width,
maxHeight: height
)
.clipped()
我認為通過使用框架和裁剪方法,我可以擺脫這個問題,但除非我使用一些平方影像,否則情況并非如此。知道什么可以解決這個問題嗎?謝謝!

uj5u.com熱心網友回復:
只需.contentShape(Rectangle())在底部添加:
Image(uiImage: image.image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(
maxWidth: width,
maxHeight: height
)
.clipped()
.contentShape(Rectangle()) // <- Here!
uj5u.com熱心網友回復:
在 Geometry Reader 中包裝內容,您需要知道寬高比,如我在下面設定的 720/460
GeometryReader { gr in
Image(uiImage: image.image)
.resizable()
.aspectRatio(720/460, contentMode: .fit)
}
.clipped()
.aspectRatio(720/460, contentMode: .fit)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/471645.html
上一篇:如何從json中獲取“@microsoft.graph.downloadUrl”,因為@是關鍵字,不能用于變數。@microsoft.graph.downloadUrl無法宣告
