Skip to content

Added dynamicMemberLookup support #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Example/SwiftUIKitExample/SwiftUIwithUIKitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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!") // <- Set UIView properties as if they were SwiftUI properties
.fixedSize() // <- Make sure the size is set
.previewLayout(.sizeThatFits)
.previewDisplayName("UILabel Preview Example")
Expand Down
2 changes: 1 addition & 1 deletion Example/SwiftUIKitExample/UIKitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftUIKitView/ModifiedUIViewContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import SwiftUI
import UIKit

@dynamicMemberLookup
public struct ModifiedUIViewContainer<ChildContainer: UIViewContaining, Child, Value>: UIViewContaining where ChildContainer.Child == Child {

let child: ChildContainer
Expand All @@ -35,5 +36,9 @@ public struct ModifiedUIViewContainer<ChildContainer: UIViewContaining, Child, V
coordinator.view?.updateContentSize()
}
}

public subscript<Value>(dynamicMember keyPath: ReferenceWritableKeyPath<Child, Value>) -> (Value) -> ModifiedUIViewContainer<Self, Child, Value> {
{ self.set(keyPath, to: $0) }
}
}

5 changes: 5 additions & 0 deletions Sources/SwiftUIKitView/UIViewContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Child: UIView> {

let viewCreator: () -> Child
Expand All @@ -24,6 +25,10 @@ public struct UIViewContainer<Child: UIView> {
self.viewCreator = viewCreator
self.layout = layout
}

public subscript<Value>(dynamicMember keyPath: ReferenceWritableKeyPath<Child, Value>) -> (Value) -> ModifiedUIViewContainer<Self, Child, Value> {
{ self.set(keyPath, to: $0) }
}
}

// MARK: Preview + UIViewRepresentable
Expand Down