{"id":22051358,"url":"https://github.com/ymm-oss/RepresentableKit","last_synced_at":"2026-04-03T12:01:34.568Z","repository":{"id":41558774,"uuid":"438108766","full_name":"ymm-oss/RepresentableKit","owner":"ymm-oss","description":"Use UIKit views inside SwiftUI, including Xcode Previews","archived":true,"fork":false,"pushed_at":"2024-07-27T00:30:29.000Z","size":31,"stargazers_count":70,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-04-02T23:38:41.947Z","etag":null,"topics":["previews","swiftui","uikit","uiviewrepresentable"],"latest_commit_sha":null,"homepage":"","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/ymm-oss.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-14T03:48:54.000Z","updated_at":"2026-04-02T02:55:14.000Z","dependencies_parsed_at":"2024-11-30T15:08:47.866Z","dependency_job_id":"54545542-5986-4403-9a0d-6c0a7340cb3f","html_url":"https://github.com/ymm-oss/RepresentableKit","commit_stats":null,"previous_names":["ymm-oss/representablekit"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/ymm-oss/RepresentableKit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymm-oss%2FRepresentableKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymm-oss%2FRepresentableKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymm-oss%2FRepresentableKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymm-oss%2FRepresentableKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ymm-oss","download_url":"https://codeload.github.com/ymm-oss/RepresentableKit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymm-oss%2FRepresentableKit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31343420,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-03T08:03:20.796Z","status":"ssl_error","status_checked_at":"2026-04-03T08:00:37.834Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","uiviewrepresentable"],"created_at":"2024-11-30T15:08:43.558Z","updated_at":"2026-04-03T12:01:34.358Z","avatar_url":"https://github.com/ymm-oss.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RepresentableKit\n\nApple provides us two ways to use UIKit views in SwiftUI:\n\n1. [`UIViewRepresentable`](https://developer.apple.com/documentation/swiftui/uiviewrepresentable)\n1. [`UIViewControllerRepresentable`](https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable)\n\nThe way those Views interact with the layout system are not really well-documented and can be a pain to implement correctly.\n\nRepresentableKit seeks to minimize the hurdle of using UIKit views, especially for previews where it is not reasonable to spend time fixing layout issues manually. We provide a wrapper over `UIViewRepresentable` that abstracts the internal SwiftUI layout process and has the most useful (opinionated) default layout handling.\n\nA minimal usage example would be previewing a simple `UILabel` with multiple lines of text. \n\n```swift\nimport SwiftUI\nimport RepresentableKit\n\nstruct UILabel_Previews: PreviewProvider {\n    static var previews: some View {\n        UIViewAdaptor {\n            let view = UILabel()\n            view.numberOfLines = 0\n            view.text = \"To love the journey is to accept no such end. I have found, through painful experience, that the most important step a person can take is always the next one.\"\n            return view\n        }\n        .padding()\n    }\n}\n```\n\nNote that had we chosen to write this naively with `UIViewRepresentable` without further customizing layout, it would render as a single line of text.\n\nFor further examples, see the provided [`Examples`](Examples/) project. \n\n# Installation\n\nUse Swift Package Manager to install RepresentableKit.\n\n- Use Xcode and add this repository as a dependency.\n- Alternatively, add this repository as a `dependency` to your `Package.swift`.\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/yumemi-inc/RepresentableKit.git\", .upToNextMajor(from: \"0.1.0\"))\n]\n```\n\n\n# UIViewRepresentable\n\nLet's look at how `UIViewRepresentable` chooses an appropriate size for the contained UIView.\n\nFirst thing we need to know is the general SwiftUI layout procedure (from [WWDC19: Building Custom Views with SwiftUI](https://developer.apple.com/videos/play/wwdc2019/237/?time=283)).\n\n1. Parent proposes a size for child\n1. Child chooses its own size\n1. Parent places child in parent's coordinate space\n\nThe second bit of knowledge is that SwiftUI views have 3 size values for each dimension: min, ideal, max. Min and max are used to clamp the proposed size, while ideal size is used when the proposed size is nil (achived by [.fixedSize()](https://developer.apple.com/documentation/swiftui/view/fixedsize()) view modifier).\n\n`UIViewRepresentable` uses Auto Layout intrinsic content size and layout priorities to map the contained UIView size. \n\n| [Intrinsic Content Size](https://developer.apple.com/documentation/uikit/uiview/1622600-intrinsiccontentsize) | [Compression Resistance](https://developer.apple.com/documentation/uikit/uiview/1622465-contentcompressionresistanceprio/) | [Hugging](https://developer.apple.com/documentation/uikit/uiview/1622556-contenthuggingpriority/) | Result size |\n|---|---|---|---|\n| (-1) | - | - | `0 … 0 … ∞` |\n| x | \u003c 750 | \u003c 750 | `0 … x … ∞` |\n| x | \u003c 750 | \u003e= 750 | `0 … x … x` |\n| x | \u003e= 750 | \u003c 750 | `x … x … ∞` |\n| x | \u003e= 750 | \u003e= 750 | `x … x … x` |\n\n- The result size is shown as `min … ideal … max`\n- `-1` = [`UIView.noIntrinsicMetric`](https://developer.apple.com/documentation/uikit/uiview/1622486-nointrinsicmetric/)\n- `750` = [`UILayoutPriority.defaultHigh`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622249-defaulthigh)\n\n`UIViewRepresentable` will also listen to [invalidateIntrinsicContentSize()](https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize/) to update its size.\n\nUsing UILabel as an example, with default priorities and intrinsic size it will map to size `x … x … ∞`, e.g. it will not become smaller than the intrinsic size, but can grow to any size; with `fixedSize()` applied it will become its intrinsic content size.\n\nMost custom UIViews, however, don't have an intrinsic size, so they will map to `0 … 0 … ∞`, e.g. growing and shrinking to any value SwiftUI proposes and vanishing (taking size 0) with `fixedSize()` applied.\n\nRepresentableKit abstracts over those concepts in two ways:\n\n1. [UIViewFlexibility](Sources/RepresentableKit/UIViewFlexibility.swift) to control min/max sizes via the layout priorities.\n1. [UIViewIdealSizeCalculator](Sources/RepresentableKit/UIViewIdealSize.swift) to control the ideal size.\n\nYou can specify a custom sizing behavior by modifying [`UIViewAdaptor.init`](Sources/RepresentableKit/UIViewAdaptor.swift) attributes.\n\n# UIViewControllerRepresentable\n\nFor now, RepresentableKit does not provide any compatibility with `UIViewControllerRepresentable`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fymm-oss%2FRepresentableKit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fymm-oss%2FRepresentableKit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fymm-oss%2FRepresentableKit/lists"}