From 70d8224085efcd9b55f38caf16073060bb9177d9 Mon Sep 17 00:00:00 2001 From: Jayson Rhynas Date: Sat, 30 Jul 2022 10:10:31 -0400 Subject: [PATCH 1/2] Added dynamicMemberLookup support Updated examples to use dynamicMemberLookup --- Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift | 6 +++--- Example/SwiftUIKitExample/UIKitView.swift | 2 +- Sources/SwiftUIKitView/ModifiedUIViewContainer.swift | 5 +++++ Sources/SwiftUIKitView/UIViewContainer.swift | 5 +++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift b/Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift index b2815a1..b146a70 100644 --- a/Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift +++ b/Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift @@ -16,8 +16,8 @@ struct SwiftUIwithUIKitView: View { VStack { // Use UIKit inside SwiftUI like this: UIViewContainer(UIKitView(), layout: .intrinsic) - .set(\.title, to: "Hello, UIKit \(integer)!") - .set(\.backgroundColor, to: UIColor(named: "swiftlee_orange")) + .title("Hello, UIKit \(integer)!") + .backgroundColor(UIColor(named: "swiftlee_orange")) .fixedSize() .navigationTitle("Use UIKit in SwiftUI") @@ -40,7 +40,7 @@ struct UILabelExample_Preview: PreviewProvider { static var previews: some View { UILabel() // <- This is a `UIKit` view. .swiftUIView(layout: .intrinsic) // <- This is a SwiftUI `View`. - .set(\.text, to: "Hello, UIKit!") // <- Use key paths for updates. + .text("Hello, UIKit!") // <- Use key paths for updates. .fixedSize() // <- Make sure the size is set .previewLayout(.sizeThatFits) .previewDisplayName("UILabel Preview Example") diff --git a/Example/SwiftUIKitExample/UIKitView.swift b/Example/SwiftUIKitExample/UIKitView.swift index 637d2ae..c89aa1a 100644 --- a/Example/SwiftUIKitExample/UIKitView.swift +++ b/Example/SwiftUIKitExample/UIKitView.swift @@ -13,7 +13,7 @@ struct UIKitView_Previews: PreviewProvider { static var previews: some View { UIKitView() .swiftUIView(layout: .intrinsic) - .set(\.title, to: "This is a UIView") + .title("This is a UIView") .preview(displayName: "A UIKit UIView preview") } } diff --git a/Sources/SwiftUIKitView/ModifiedUIViewContainer.swift b/Sources/SwiftUIKitView/ModifiedUIViewContainer.swift index e3532fd..df8f277 100644 --- a/Sources/SwiftUIKitView/ModifiedUIViewContainer.swift +++ b/Sources/SwiftUIKitView/ModifiedUIViewContainer.swift @@ -9,6 +9,7 @@ import Foundation import SwiftUI import UIKit +@dynamicMemberLookup public struct ModifiedUIViewContainer: UIViewContaining where ChildContainer.Child == Child { let child: ChildContainer @@ -35,5 +36,9 @@ public struct ModifiedUIViewContainer(dynamicMember keyPath: ReferenceWritableKeyPath) -> (Value) -> ModifiedUIViewContainer { + { self.set(keyPath, to: $0) } + } } diff --git a/Sources/SwiftUIKitView/UIViewContainer.swift b/Sources/SwiftUIKitView/UIViewContainer.swift index 8693002..f1b7314 100644 --- a/Sources/SwiftUIKitView/UIViewContainer.swift +++ b/Sources/SwiftUIKitView/UIViewContainer.swift @@ -11,6 +11,7 @@ import SwiftUI /// A container for UIKit `UIView` elements. Conforms to the `UIViewRepresentable` protocol to allow conversion into SwiftUI `View`s. @available(iOS 13.0, *) +@dynamicMemberLookup public struct UIViewContainer { let viewCreator: () -> Child @@ -24,6 +25,10 @@ public struct UIViewContainer { self.viewCreator = viewCreator self.layout = layout } + + public subscript(dynamicMember keyPath: ReferenceWritableKeyPath) -> (Value) -> ModifiedUIViewContainer { + { self.set(keyPath, to: $0) } + } } // MARK: Preview + UIViewRepresentable From 7b60f10bee31146a8f0b795efaf79dfcd879fec2 Mon Sep 17 00:00:00 2001 From: Jayson Rhynas Date: Sat, 30 Jul 2022 10:18:10 -0400 Subject: [PATCH 2/2] Updated comment to match --- Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift b/Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift index b146a70..dd7b64a 100644 --- a/Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift +++ b/Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift @@ -40,7 +40,7 @@ struct UILabelExample_Preview: PreviewProvider { static var previews: some View { UILabel() // <- This is a `UIKit` view. .swiftUIView(layout: .intrinsic) // <- This is a SwiftUI `View`. - .text("Hello, UIKit!") // <- Use key paths for updates. + .text("Hello, UIKit!") // <- Set UIView properties as if they were SwiftUI properties .fixedSize() // <- Make sure the size is set .previewLayout(.sizeThatFits) .previewDisplayName("UILabel Preview Example")