我有一个奇怪的问题,如果我制作的一个可调整大小的图像(自定义视图)不在NavigationButton中,则它可以正常工作,但是如果在其中,则该图像的大小仍然合适,但会变成全蓝色。有人知道怎么了吗?
struct DynamicallyResizableImage: View {
let imageName: String
let height: CGFloat
let width: CGFloat
init(imageName: String, height: CGFloat? = nil, width: CGFloat? = nil) {
self.imageName = imageName
let imageSize = UIImage(named: imageName)!.size
guard let height = height else {
if let width = width {
self.width = width
self.height = imageSize.height * (width/imageSize.width)
return
}
self.height = 0
self.width = 0
return
}
if let width = width {
self.height = height
self.width = width
return
}
self.height = height
self.width = imageSize.width * (height/imageSize.height)
}
var body: some View {
Image(imageName)
.resizable()
.frame(width: width, height: height)
}
}
NavigationButton(destination: PicturePreviewView(imageName: "IMG_4955")) {
DynamicallyResizableImage(imageName: "IMG_4955", height: 200)
}
如果未嵌入NavigationButton中,则一切正常。嵌入到NavigationButton中后,图像将变成蓝色。
有人知道如何解决它吗?谢谢!!!
答案 0 :(得分:1)
所以我在同一个redditor的帮助下修复了它。在自定义视图的内部,我必须像这样向Image添加renderMode:
Image(imageName)
.renderingMode(.original)
.resizable()
.frame(width: width, height: height)