{"id":16322259,"url":"https://github.com/p-x9/cocoaui","last_synced_at":"2025-03-20T22:31:23.192Z","repository":{"id":149984522,"uuid":"622544913","full_name":"p-x9/CocoaUI","owner":"p-x9","description":"☕️ Obtain and customize UIKit/Cocoa objects from SwiftUI components.","archived":false,"fork":false,"pushed_at":"2023-07-16T14:05:05.000Z","size":45,"stargazers_count":34,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-11T22:50:28.913Z","etag":null,"topics":["cocoa","swiftpackage","swiftpackagemanager","swiftpm","swiftui","uikit"],"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/p-x9.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":"2023-04-02T12:42:15.000Z","updated_at":"2024-10-11T15:50:07.000Z","dependencies_parsed_at":"2024-10-28T09:07:50.548Z","dependency_job_id":"aff33f10-7b30-46dd-ac73-a01454888abf","html_url":"https://github.com/p-x9/CocoaUI","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p-x9%2FCocoaUI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p-x9%2FCocoaUI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p-x9%2FCocoaUI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/p-x9%2FCocoaUI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/p-x9","download_url":"https://codeload.github.com/p-x9/CocoaUI/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244703847,"owners_count":20496187,"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":["cocoa","swiftpackage","swiftpackagemanager","swiftpm","swiftui","uikit"],"created_at":"2024-10-10T22:50:25.752Z","updated_at":"2025-03-20T22:31:23.187Z","avatar_url":"https://github.com/p-x9.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CocoaUI\n\nObtain and customize UIKit/Cocoa objects from SwiftUI components.\n\n\u003c!-- # Badges --\u003e\n\n[![Github issues](https://img.shields.io/github/issues/p-x9/CocoaUI)](https://github.com/p-x9/CocoaUI/issues)\n[![Github forks](https://img.shields.io/github/forks/p-x9/CocoaUI)](https://github.com/p-x9/CocoaUI/network/members)\n[![Github stars](https://img.shields.io/github/stars/p-x9/CocoaUI)](https://github.com/p-x9/CocoaUI/stargazers)\n[![Github top language](https://img.shields.io/github/languages/top/p-x9/CocoaUI)](https://github.com/p-x9/CocoaUI/)\n\n## Demo\nFor example, Slider uses UISlider internally.\nTherefore, it can be customized by directly referencing the UISlider object as follows.\n\n```swift\n Slider(value: $value)\n    .cocoa { slider in // UISider\n        slider.setThumbImage(.init(systemName: \"swift\"), for: .normal)\n    }\n```\n![Slider](https://user-images.githubusercontent.com/50244599/229353608-86eb9a3c-815e-4919-9f44-1cc35d244d7e.png)\n\n## Document\nFor components conforming to the protocol named `DefaultCocoaViewBridging`, you can get UIKit/Cocoa objects as follows.\n`DefaultCocoaViewBridging` gets the UIView object from SwiftUI.View.\nIn contrast, `DefaultCocoaControllerBridging` gets the UIViewController object.\n\u003c/br\u003e\nThe CocoaBriding protocol defines a `DefaultCocoaType`.　　\nFor example, for Toggle, the DefaultCocoaType is UISwitch(iOS).　　\nIt can be handled as follows\n\n```swift\n Toggle(\"Hello\", isOn: .constant(true))\n    .cocoa { `switch` in\n        `switch`.onTintColor = .red\n    }\n```\n### Specify type\nHowever, if the ToggleStyle is set to `Button`, `UIButton` is used internally instead of `UISwitch`.\nFor such cases, it is also possible to retrieve the data by specifying the type as follows.\n\n```swift\nToggle(\"Hello\", isOn: .constant(true))\n    .cocoa(for: UIButton.self) { button in\n       button.layer.borderWidth = 1\n    }\n```\nThe method of specifying the type is defined in SwiftUI.View's and does not need to conform to the `DefaultCocoaViewBridging` or `DefaultCocoaControllerBridging ` protocols.\nIf the specified type is not found, the closure will not be called.\n\n### Support additional component\n```swift\nextension XXView: DefaultCocoaViewBridging { // confirms `DefaultCocoaViewBridging`\n    public typealias DefaultCocoaViewType = XXCocoaView // UIKit/Cocoa type\n}\n\nextension YYView: DefaultCocoaViewControllerBridging { // confirms `DefaultCocoaViewControllerBridging`\n    public typealias DefaultCocoaControllerType = YYCocoaViewController // UIKit/Cocoa type\n}\n```\n\n### LifeCycle Event Modifiers\nIn some View lifecycle events, a modifier is provided to retrieve the obtained UIKit/Cocoa object.\nAs an example, the following code hides the tabBar on push and redisplays it on pop.\n```swift\nTabView {\n    NavigationView {\n        List(0..\u003c100) { i in\n            NavigationLink {\n                Text(\"Detail: \\(i)\")\n                    .cocoa(for: CocoaViewController.self) { vc in\n                        print(vc)\n                    }\n                    .onViewWillAppear { vc in\n                        // Hide TabBar\n                        vc?.tabBarController?.tabBar.isHidden = true\n                    }\n                    .onViewWillDisappear { vc in\n                        // Show TabBar\n                        vc?.tabBarController?.tabBar.isHidden = false\n                    }\n            } label: {\n                Text(\"Row: \\(i)\")\n            }\n        }\n    }\n}\n```\n\nThe following modifiers are available.\n- onViewWillAppear\n- onViewDidLoad\n- onViewWillDisappear\n- onViewDidDisAppear\n\n## SwiftUI and Cocoa correspondence table\nThis may vary depending on the operating system and usage conditions.\n\n|SwiftUI|style|UIKit(iOS)|Cocoa(macOS)|UIKit(tvOS)|\n|:----:|:----:|:----:|:----:|:----:|\n|ScrollView|-| UIScrollView|NSScrollView|UIScrollView|\n|List|-| UICollectionView(\u003e=iOS16) UITableView(\u003ciOS16)|NSTableView|UITableView|\n|Form|-| UICollectionView(\u003e=iOS16) UITableView(\u003ciOS16)|?|UITableView|\n|TextField|-| UITextField|NSTextField|UITextField|\n|SecureField|-| UITextField|NSTextField|UITextField|\n|Slider|-|UISlider|NSSlider|-|\n|Stepper|-|UIStepper|NSStepper|-|\n|Picker|Wheel|UIPickerView|NSButton|-|\n||Inline|UIPickerView|?|UISegmentedControl|\n||Segmented|UISegmentedControl|NSSegmentedControl|UISegmentedControl|\n||Menu|?|NSButton|-|\n|DatePicker|-|UIDatePicker|NSDatePicker|-|\n|MultiDatePicker|-|UICalendarView|-|-|\n|ColorPicker|-|UIColorWell|NSColorWell|-|\n|Toggle|.switch|UISwitch| NSSwitch |-|\n| |.button|UIButton| NSButton |-|\n| |.checkbox|-| NSButton |-|\n|TextEditor|-|UITextView|NSTextView|-|\n|Button|-|-|NSButton|-|\n|ProgressView|.linear|UIProgressView|NSProgressIndicator|UIProgressView|\n||.circular|UIActivityIndicatorView|NSProgressIndicator|UIActivityIndicatorView|\n|TabView|-|UITabBarController|NSTabView|UITabBarController|\n|NavigationView|DoubleColumn|UISplitViewController|NSSplitView|-|\n||Stack|UINavigationController|-|UINavigationController|\n|NavigationStack|-|UINavigationController|?|UINavigationController|\n|NavgationSplitView|-|UISplitViewController|NSSplitView|-|\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp-x9%2Fcocoaui","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fp-x9%2Fcocoaui","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fp-x9%2Fcocoaui/lists"}