From 149f265738eb10a8f8fc3a18e6c712fa2a0b2537 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 30 Oct 2019 01:56:00 +0800 Subject: [PATCH] Using the intrinsicContentSize on wrapper container, try to fix the aspect ratio sizing issue, hope this is the final change --- SDWebImageSwiftUI/Classes/ImageViewWrapper.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/SDWebImageSwiftUI/Classes/ImageViewWrapper.swift b/SDWebImageSwiftUI/Classes/ImageViewWrapper.swift index 469519c6..8d22217f 100644 --- a/SDWebImageSwiftUI/Classes/ImageViewWrapper.swift +++ b/SDWebImageSwiftUI/Classes/ImageViewWrapper.swift @@ -43,6 +43,19 @@ public class AnimatedImageViewWrapper : PlatformView { } #endif + public override var intrinsicContentSize: CGSize { + /// Used to fix SwiftUI layout issue when image view is aspectFit/aspectFill :) + /// The container will measure its own size with 1:1 firstly, then change image view size, which cause image view sizing smaller than expected + /// Instead, the container should firstly return its own size with image view's aspect ratio + let size = wrapped.intrinsicContentSize + if size.width > 0 && size.height > 0 { + let aspectRatio = size.height / size.width + return CGSize(width: 1, height: 1 * aspectRatio) + } else { + return super.intrinsicContentSize + } + } + public override init(frame frameRect: CGRect) { super.init(frame: frameRect) addSubview(wrapped)