Skip to content

Commit 149f265

Browse files
committed
Using the intrinsicContentSize on wrapper container, try to fix the aspect ratio sizing issue, hope this is the final change
1 parent 3ea6e12 commit 149f265

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

SDWebImageSwiftUI/Classes/ImageViewWrapper.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ public class AnimatedImageViewWrapper : PlatformView {
4343
}
4444
#endif
4545

46+
public override var intrinsicContentSize: CGSize {
47+
/// Used to fix SwiftUI layout issue when image view is aspectFit/aspectFill :)
48+
/// The container will measure its own size with 1:1 firstly, then change image view size, which cause image view sizing smaller than expected
49+
/// Instead, the container should firstly return its own size with image view's aspect ratio
50+
let size = wrapped.intrinsicContentSize
51+
if size.width > 0 && size.height > 0 {
52+
let aspectRatio = size.height / size.width
53+
return CGSize(width: 1, height: 1 * aspectRatio)
54+
} else {
55+
return super.intrinsicContentSize
56+
}
57+
}
58+
4659
public override init(frame frameRect: CGRect) {
4760
super.init(frame: frameRect)
4861
addSubview(wrapped)

0 commit comments

Comments
 (0)