{"id":18269915,"url":"https://github.com/AvdLee/SwiftUIKitView","last_synced_at":"2025-04-04T23:31:37.071Z","repository":{"id":40804363,"uuid":"325021005","full_name":"AvdLee/SwiftUIKitView","owner":"AvdLee","description":"Easily use UIKit views in your SwiftUI applications. Create Xcode Previews for UIView elements","archived":false,"fork":false,"pushed_at":"2022-07-30T14:18:15.000Z","size":84,"stargazers_count":763,"open_issues_count":3,"forks_count":25,"subscribers_count":12,"default_branch":"main","last_synced_at":"2024-10-20T21:23:14.759Z","etag":null,"topics":["previews","swiftui","uikit","uiview","uiviewrepresentable","xcode"],"latest_commit_sha":null,"homepage":"https://www.avanderlee.com","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AvdLee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["AvdLee"]}},"created_at":"2020-12-28T13:32:25.000Z","updated_at":"2024-10-05T15:49:18.000Z","dependencies_parsed_at":"2022-08-10T00:52:46.154Z","dependency_job_id":null,"html_url":"https://github.com/AvdLee/SwiftUIKitView","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvdLee%2FSwiftUIKitView","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvdLee%2FSwiftUIKitView/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvdLee%2FSwiftUIKitView/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvdLee%2FSwiftUIKitView/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AvdLee","download_url":"https://codeload.github.com/AvdLee/SwiftUIKitView/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266476,"owners_count":20910831,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["previews","swiftui","uikit","uiview","uiviewrepresentable","xcode"],"created_at":"2024-11-05T11:37:52.525Z","updated_at":"2025-04-04T23:31:37.053Z","avatar_url":"https://github.com/AvdLee.png","language":"Swift","readme":"# SwiftUIKitView\n![Swift Version](https://img.shields.io/badge/Swift-5.5-F16D39.svg?style=flat) ![Dependency frameworks](https://img.shields.io/badge/Supports-_Swift_Package_Manager-F16D39.svg?style=flat) [![Twitter](https://img.shields.io/badge/twitter-@Twannl-blue.svg?style=flat)](https://twitter.com/twannl)\n\nEasily use UIKit views in SwiftUI.\n\n- Convert `UIView` to SwiftUI `View`\n- Create Xcode Previews from `UIView` elements\n- SwiftUI functional updating `UIView` properties using a protocol with [Associated Types](https://www.avanderlee.com/swift/associated-types-protocols/).\n\nYou can read more about [Getting started with UIKit in SwiftUI and visa versa](https://www.avanderlee.com/swiftui/integrating-swiftui-with-uikit/).\n\n## Examples\n\n### Using SwiftUIKitView in Production Code\nUsing a `UIKit` view directly in SwiftUI for production code requires you to use:\n\n```swift\nUIViewContainer(\u003cYOUR UIKit View\u003e, layout: \u003cYOUR LAYOUT PREFERENCE\u003e)\n```\n\nThis is to prevent a UIKit view from being redrawn on every SwiftUI view redraw.\n\n```swift\nimport SwiftUI\nimport SwiftUIKitView\n\nstruct SwiftUIwithUIKitView: View {\n    var body: some View {\n        NavigationView {\n            UIViewContainer(UILabel(), layout: .intrinsic) // \u003c- This can be any `UIKit` view.\n                .set(\\.text, to: \"Hello, UIKit!\") // \u003c- Use key paths for updates.\n                .set(\\.backgroundColor, to: UIColor(named: \"swiftlee_orange\"))\n                .fixedSize()\n                .navigationTitle(\"Use UIKit in SwiftUI\")\n        }\n    }\n}\n```\n\n### Using `SwiftUIKitView` in Previews\nPerformance in Previews is less important, it's being redrawn either way.\n\nTherefore, you can use the more convenient  `swiftUIView()` modifier:\n\n```swift\nUILabel() // \u003c- This is a `UIKit` view.\n    .swiftUIView(layout: .intrinsic) // \u003c- This is returning a SwiftUI `View`.\n```\n\nCreating a preview provider for a `UIView` looks as follows:\n\n```swift\nimport SwiftUI\nimport SwiftUIKitView\n\nstruct UILabelExample_Preview: PreviewProvider {\n    static var previews: some View {\n        UILabel() // \u003c- This is a `UIKit` view.\n            .swiftUIView(layout: .intrinsic) // \u003c- This is a SwiftUI `View`.\n            .set(\\.text, to: \"Hello, UIKit!\") // \u003c- Use key paths for updates.\n            .fixedSize() // \u003c- Make sure the size is set\n            .previewLayout(.sizeThatFits)\n            .previewDisplayName(\"UILabel Preview Example\")\n    }\n}\n```\n\nWhich results in the following preview:\n\n\u003cimg src=\"Assets/uikit_uilabel_preview.png\" width=\"750\"/\u003e\n\n### KeyPath updating\n\nThis framework also comes with a `KeyPathReferenceWritable` protocol that allows to update objects using functions and writable KeyPath references:\n\n```swift\n/// Defines a type that is configurable using reference writeable keypaths.\npublic protocol KeyPathReferenceWritable {\n    associatedtype T\n    associatedtype U\n    \n    func set\u003cValue\u003e(_ keyPath: ReferenceWritableKeyPath\u003cT, Value\u003e, to value: Value) -\u003e U\n}\n\npublic extension KeyPathReferenceWritable {\n    func set\u003cValue\u003e(_ keyPath: ReferenceWritableKeyPath\u003cSelf, Value\u003e, to value: Value) -\u003e Self {\n        self[keyPath: keyPath] = value\n        return self\n    }\n}\n\n/// Add inheritance for NSObject types to make the methods accessible for many default types.\nextension NSObject: KeyPathReferenceWritable { }\n```\n\nThis can be used as follows:\n\n```swift\nUILabel()\n    .set(\\.text, to: \"Example\")\n```\n\nAnd allows to easily build up SwiftUI style view configurations to keep the same readability when working in SwiftUI.\n\n## Installation\n\n### Swift Package Manager\n\nThe [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler. It is in early development, but this SDK does support its use on supported platforms. \n\nOnce you have your Swift package set up, adding the SDK as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/AvdLee/SwiftUIKitView.git\", .upToNextMajor(from: \"2.0.0\"))\n]\n```\n\n## Communication\n\n- If you **found a bug**, open an [issue](https://github.com/AvdLee/SwiftUIKitView/issues).\n- If you **have a feature request**, open an [issue](https://github.com/AvdLee/SwiftUIKitView/issues).\n- If you **want to contribute**, submit a [pull request](https://github.com/AvdLee/SwiftUIKitView/pulls).\n\n\n## License\n\n**SwiftUIKitView** is available under the MIT license, and uses source code from open source projects. See the [LICENSE](https://github.com/AvdLee/SwiftUIKitView/blob/main/LICENSE) file for more info.\n\n## Author\n\nThis project is originally created by [Antoine van der Lee](https://www.twitter.com/twannl). I'm open for contributions of any kind to make this project even better.\n","funding_links":["https://github.com/sponsors/AvdLee"],"categories":["xcode","Libraries"],"sub_categories":["Extensions"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAvdLee%2FSwiftUIKitView","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAvdLee%2FSwiftUIKitView","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAvdLee%2FSwiftUIKitView/lists"}