From 2bec1f9d3fe0dbd6d81afd8543299f236a560cf3 Mon Sep 17 00:00:00 2001 From: Antoine van der Lee Date: Thu, 7 Jan 2021 16:48:47 +0100 Subject: [PATCH] Better support for intrinsic sizes --- Sources/SwiftUIKitView/UIViewContainer.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftUIKitView/UIViewContainer.swift b/Sources/SwiftUIKitView/UIViewContainer.swift index 2b0b126..cb9b47c 100644 --- a/Sources/SwiftUIKitView/UIViewContainer.swift +++ b/Sources/SwiftUIKitView/UIViewContainer.swift @@ -63,6 +63,16 @@ public struct UIViewContainer: Identifiable { public init(_ view: @autoclosure () -> Child, layout: Layout = .intrinsic) { self.view = view() self.layout = layout + + switch layout { + case .intrinsic: + return + case .fixed(let size): + self.view.widthAnchor.constraint(equalToConstant: size.width).isActive = true + self.view.heightAnchor.constraint(equalToConstant: size.height).isActive = true + case .fixedWidth(let width): + self.view.widthAnchor.constraint(equalToConstant: width).isActive = true + } } /// Applies the correct size to the SwiftUI `View` container. @@ -87,8 +97,6 @@ public struct UIViewContainer: Identifiable { extension UIViewContainer: UIViewRepresentable { public func makeUIView(context: Context) -> UIView { - view.widthAnchor.constraint(equalToConstant: size.width).isActive = true - view.heightAnchor.constraint(equalToConstant: size.height).isActive = true return view }