├── .gitignore ├── Examples ├── Examples.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── Sources │ ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── ExamplesApp.swift │ ├── Previews │ ├── CustomUIView+Previews.swift │ ├── UIImageView+Previews.swift │ ├── UILabel+Previews.swift │ └── UISlider+Previews.swift │ └── Utility │ ├── SizeAdjustView.swift │ └── SizeGetter.swift ├── LICENSE ├── Package.swift ├── README.md └── Sources └── RepresentableKit ├── UIViewAdaptor.swift ├── UIViewFlexibility.swift └── UIViewIdealSize.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | 92 | .DS_Store 93 | .AppleDouble 94 | .LSOverride 95 | 96 | # Icon must end with two \r 97 | Icon 98 | 99 | # Thumbnails 100 | ._* 101 | 102 | # Files that might appear in the root of a volume 103 | .DocumentRevisions-V100 104 | .fseventsd 105 | .Spotlight-V100 106 | .TemporaryItems 107 | .Trashes 108 | .VolumeIcon.icns 109 | .com.apple.timemachine.donotpresent 110 | 111 | # Directories potentially created on remote AFP share 112 | .AppleDB 113 | .AppleDesktop 114 | Network Trash Folder 115 | Temporary Items 116 | .apdisk -------------------------------------------------------------------------------- /Examples/Examples.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 61EA31A9275A247500AF136E /* ExamplesApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61EA31A8275A247500AF136E /* ExamplesApp.swift */; }; 11 | 61EA31AD275A247600AF136E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 61EA31AC275A247600AF136E /* Assets.xcassets */; }; 12 | 61EA31C5275A267D00AF136E /* UIImageView+Previews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61EA31C1275A267C00AF136E /* UIImageView+Previews.swift */; }; 13 | 61EA31C6275A267D00AF136E /* UILabel+Previews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61EA31C2275A267C00AF136E /* UILabel+Previews.swift */; }; 14 | 61EA31C7275A267D00AF136E /* CustomUIView+Previews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61EA31C3275A267C00AF136E /* CustomUIView+Previews.swift */; }; 15 | 61EA31C8275A267D00AF136E /* UISlider+Previews.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61EA31C4275A267D00AF136E /* UISlider+Previews.swift */; }; 16 | 61EA31D4275A2A5000AF136E /* SizeAdjustView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61EA31D3275A2A5000AF136E /* SizeAdjustView.swift */; }; 17 | 61EA31D6275A31FF00AF136E /* SizeGetter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61EA31D5275A31FF00AF136E /* SizeGetter.swift */; }; 18 | A351EA8A27683F98003129AC /* RepresentableKit in Frameworks */ = {isa = PBXBuildFile; productRef = A351EA8927683F98003129AC /* RepresentableKit */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXFileReference section */ 22 | 61EA31A5275A247500AF136E /* Examples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Examples.app; sourceTree = BUILT_PRODUCTS_DIR; }; 23 | 61EA31A8275A247500AF136E /* ExamplesApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExamplesApp.swift; sourceTree = ""; }; 24 | 61EA31AC275A247600AF136E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | 61EA31C1275A267C00AF136E /* UIImageView+Previews.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImageView+Previews.swift"; sourceTree = ""; }; 26 | 61EA31C2275A267C00AF136E /* UILabel+Previews.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UILabel+Previews.swift"; sourceTree = ""; }; 27 | 61EA31C3275A267C00AF136E /* CustomUIView+Previews.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CustomUIView+Previews.swift"; sourceTree = ""; }; 28 | 61EA31C4275A267D00AF136E /* UISlider+Previews.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UISlider+Previews.swift"; sourceTree = ""; }; 29 | 61EA31D3275A2A5000AF136E /* SizeAdjustView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SizeAdjustView.swift; sourceTree = ""; }; 30 | 61EA31D5275A31FF00AF136E /* SizeGetter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SizeGetter.swift; sourceTree = ""; }; 31 | A351EA8827683F91003129AC /* RepresentableKit */ = {isa = PBXFileReference; lastKnownFileType = folder; name = RepresentableKit; path = ..; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | 61EA31A2275A247500AF136E /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | A351EA8A27683F98003129AC /* RepresentableKit in Frameworks */, 40 | ); 41 | runOnlyForDeploymentPostprocessing = 0; 42 | }; 43 | /* End PBXFrameworksBuildPhase section */ 44 | 45 | /* Begin PBXGroup section */ 46 | 61EA319C275A247500AF136E = { 47 | isa = PBXGroup; 48 | children = ( 49 | 61EA31A7275A247500AF136E /* Sources */, 50 | 61EA31C9275A273A00AF136E /* Packages */, 51 | 61EA31A6275A247500AF136E /* Products */, 52 | 61EA31D0275A2A1400AF136E /* Frameworks */, 53 | ); 54 | sourceTree = ""; 55 | }; 56 | 61EA31A6275A247500AF136E /* Products */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 61EA31A5275A247500AF136E /* Examples.app */, 60 | ); 61 | name = Products; 62 | sourceTree = ""; 63 | }; 64 | 61EA31A7275A247500AF136E /* Sources */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | A351EA8C27684308003129AC /* Utility */, 68 | A351EA8B276842F5003129AC /* Previews */, 69 | 61EA31A8275A247500AF136E /* ExamplesApp.swift */, 70 | 61EA31AC275A247600AF136E /* Assets.xcassets */, 71 | ); 72 | path = Sources; 73 | sourceTree = ""; 74 | }; 75 | 61EA31C9275A273A00AF136E /* Packages */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | A351EA8827683F91003129AC /* RepresentableKit */, 79 | ); 80 | name = Packages; 81 | sourceTree = ""; 82 | }; 83 | 61EA31D0275A2A1400AF136E /* Frameworks */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | ); 87 | name = Frameworks; 88 | sourceTree = ""; 89 | }; 90 | A351EA8B276842F5003129AC /* Previews */ = { 91 | isa = PBXGroup; 92 | children = ( 93 | 61EA31C3275A267C00AF136E /* CustomUIView+Previews.swift */, 94 | 61EA31C1275A267C00AF136E /* UIImageView+Previews.swift */, 95 | 61EA31C2275A267C00AF136E /* UILabel+Previews.swift */, 96 | 61EA31C4275A267D00AF136E /* UISlider+Previews.swift */, 97 | ); 98 | path = Previews; 99 | sourceTree = ""; 100 | }; 101 | A351EA8C27684308003129AC /* Utility */ = { 102 | isa = PBXGroup; 103 | children = ( 104 | 61EA31D5275A31FF00AF136E /* SizeGetter.swift */, 105 | 61EA31D3275A2A5000AF136E /* SizeAdjustView.swift */, 106 | ); 107 | path = Utility; 108 | sourceTree = ""; 109 | }; 110 | /* End PBXGroup section */ 111 | 112 | /* Begin PBXNativeTarget section */ 113 | 61EA31A4275A247500AF136E /* Examples */ = { 114 | isa = PBXNativeTarget; 115 | buildConfigurationList = 61EA31B3275A247600AF136E /* Build configuration list for PBXNativeTarget "Examples" */; 116 | buildPhases = ( 117 | 61EA31A1275A247500AF136E /* Sources */, 118 | 61EA31A2275A247500AF136E /* Frameworks */, 119 | 61EA31A3275A247500AF136E /* Resources */, 120 | ); 121 | buildRules = ( 122 | ); 123 | dependencies = ( 124 | ); 125 | name = Examples; 126 | packageProductDependencies = ( 127 | A351EA8927683F98003129AC /* RepresentableKit */, 128 | ); 129 | productName = Examples; 130 | productReference = 61EA31A5275A247500AF136E /* Examples.app */; 131 | productType = "com.apple.product-type.application"; 132 | }; 133 | /* End PBXNativeTarget section */ 134 | 135 | /* Begin PBXProject section */ 136 | 61EA319D275A247500AF136E /* Project object */ = { 137 | isa = PBXProject; 138 | attributes = { 139 | BuildIndependentTargetsInParallel = 1; 140 | LastSwiftUpdateCheck = 1310; 141 | LastUpgradeCheck = 1310; 142 | TargetAttributes = { 143 | 61EA31A4275A247500AF136E = { 144 | CreatedOnToolsVersion = 13.1; 145 | }; 146 | }; 147 | }; 148 | buildConfigurationList = 61EA31A0275A247500AF136E /* Build configuration list for PBXProject "Examples" */; 149 | compatibilityVersion = "Xcode 13.0"; 150 | developmentRegion = en; 151 | hasScannedForEncodings = 0; 152 | knownRegions = ( 153 | en, 154 | Base, 155 | ); 156 | mainGroup = 61EA319C275A247500AF136E; 157 | productRefGroup = 61EA31A6275A247500AF136E /* Products */; 158 | projectDirPath = ""; 159 | projectRoot = ""; 160 | targets = ( 161 | 61EA31A4275A247500AF136E /* Examples */, 162 | ); 163 | }; 164 | /* End PBXProject section */ 165 | 166 | /* Begin PBXResourcesBuildPhase section */ 167 | 61EA31A3275A247500AF136E /* Resources */ = { 168 | isa = PBXResourcesBuildPhase; 169 | buildActionMask = 2147483647; 170 | files = ( 171 | 61EA31AD275A247600AF136E /* Assets.xcassets in Resources */, 172 | ); 173 | runOnlyForDeploymentPostprocessing = 0; 174 | }; 175 | /* End PBXResourcesBuildPhase section */ 176 | 177 | /* Begin PBXSourcesBuildPhase section */ 178 | 61EA31A1275A247500AF136E /* Sources */ = { 179 | isa = PBXSourcesBuildPhase; 180 | buildActionMask = 2147483647; 181 | files = ( 182 | 61EA31C5275A267D00AF136E /* UIImageView+Previews.swift in Sources */, 183 | 61EA31D6275A31FF00AF136E /* SizeGetter.swift in Sources */, 184 | 61EA31C7275A267D00AF136E /* CustomUIView+Previews.swift in Sources */, 185 | 61EA31A9275A247500AF136E /* ExamplesApp.swift in Sources */, 186 | 61EA31C6275A267D00AF136E /* UILabel+Previews.swift in Sources */, 187 | 61EA31D4275A2A5000AF136E /* SizeAdjustView.swift in Sources */, 188 | 61EA31C8275A267D00AF136E /* UISlider+Previews.swift in Sources */, 189 | ); 190 | runOnlyForDeploymentPostprocessing = 0; 191 | }; 192 | /* End PBXSourcesBuildPhase section */ 193 | 194 | /* Begin XCBuildConfiguration section */ 195 | 61EA31B1275A247600AF136E /* Debug */ = { 196 | isa = XCBuildConfiguration; 197 | buildSettings = { 198 | ALWAYS_SEARCH_USER_PATHS = NO; 199 | CLANG_ANALYZER_NONNULL = YES; 200 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 201 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 202 | CLANG_CXX_LIBRARY = "libc++"; 203 | CLANG_ENABLE_MODULES = YES; 204 | CLANG_ENABLE_OBJC_ARC = YES; 205 | CLANG_ENABLE_OBJC_WEAK = YES; 206 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 207 | CLANG_WARN_BOOL_CONVERSION = YES; 208 | CLANG_WARN_COMMA = YES; 209 | CLANG_WARN_CONSTANT_CONVERSION = YES; 210 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 211 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 212 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 213 | CLANG_WARN_EMPTY_BODY = YES; 214 | CLANG_WARN_ENUM_CONVERSION = YES; 215 | CLANG_WARN_INFINITE_RECURSION = YES; 216 | CLANG_WARN_INT_CONVERSION = YES; 217 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 218 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 219 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 220 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 221 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 222 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 223 | CLANG_WARN_STRICT_PROTOTYPES = YES; 224 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 225 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 226 | CLANG_WARN_UNREACHABLE_CODE = YES; 227 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 228 | COPY_PHASE_STRIP = NO; 229 | DEBUG_INFORMATION_FORMAT = dwarf; 230 | ENABLE_STRICT_OBJC_MSGSEND = YES; 231 | ENABLE_TESTABILITY = YES; 232 | GCC_C_LANGUAGE_STANDARD = gnu11; 233 | GCC_DYNAMIC_NO_PIC = NO; 234 | GCC_NO_COMMON_BLOCKS = YES; 235 | GCC_OPTIMIZATION_LEVEL = 0; 236 | GCC_PREPROCESSOR_DEFINITIONS = ( 237 | "DEBUG=1", 238 | "$(inherited)", 239 | ); 240 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 241 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 242 | GCC_WARN_UNDECLARED_SELECTOR = YES; 243 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 244 | GCC_WARN_UNUSED_FUNCTION = YES; 245 | GCC_WARN_UNUSED_VARIABLE = YES; 246 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 247 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 248 | MTL_FAST_MATH = YES; 249 | ONLY_ACTIVE_ARCH = YES; 250 | SDKROOT = iphoneos; 251 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 252 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 253 | }; 254 | name = Debug; 255 | }; 256 | 61EA31B2275A247600AF136E /* Release */ = { 257 | isa = XCBuildConfiguration; 258 | buildSettings = { 259 | ALWAYS_SEARCH_USER_PATHS = NO; 260 | CLANG_ANALYZER_NONNULL = YES; 261 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 262 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 263 | CLANG_CXX_LIBRARY = "libc++"; 264 | CLANG_ENABLE_MODULES = YES; 265 | CLANG_ENABLE_OBJC_ARC = YES; 266 | CLANG_ENABLE_OBJC_WEAK = YES; 267 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 268 | CLANG_WARN_BOOL_CONVERSION = YES; 269 | CLANG_WARN_COMMA = YES; 270 | CLANG_WARN_CONSTANT_CONVERSION = YES; 271 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 272 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 273 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 274 | CLANG_WARN_EMPTY_BODY = YES; 275 | CLANG_WARN_ENUM_CONVERSION = YES; 276 | CLANG_WARN_INFINITE_RECURSION = YES; 277 | CLANG_WARN_INT_CONVERSION = YES; 278 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 279 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 280 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 281 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 282 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 283 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 284 | CLANG_WARN_STRICT_PROTOTYPES = YES; 285 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 286 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 287 | CLANG_WARN_UNREACHABLE_CODE = YES; 288 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 289 | COPY_PHASE_STRIP = NO; 290 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 291 | ENABLE_NS_ASSERTIONS = NO; 292 | ENABLE_STRICT_OBJC_MSGSEND = YES; 293 | GCC_C_LANGUAGE_STANDARD = gnu11; 294 | GCC_NO_COMMON_BLOCKS = YES; 295 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 296 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 297 | GCC_WARN_UNDECLARED_SELECTOR = YES; 298 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 299 | GCC_WARN_UNUSED_FUNCTION = YES; 300 | GCC_WARN_UNUSED_VARIABLE = YES; 301 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 302 | MTL_ENABLE_DEBUG_INFO = NO; 303 | MTL_FAST_MATH = YES; 304 | SDKROOT = iphoneos; 305 | SWIFT_COMPILATION_MODE = wholemodule; 306 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 307 | VALIDATE_PRODUCT = YES; 308 | }; 309 | name = Release; 310 | }; 311 | 61EA31B4275A247600AF136E /* Debug */ = { 312 | isa = XCBuildConfiguration; 313 | buildSettings = { 314 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 315 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 316 | CODE_SIGN_STYLE = Automatic; 317 | CURRENT_PROJECT_VERSION = 1; 318 | DEVELOPMENT_ASSET_PATHS = ""; 319 | DEVELOPMENT_TEAM = FK4VRNJVL7; 320 | ENABLE_PREVIEWS = YES; 321 | GENERATE_INFOPLIST_FILE = YES; 322 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 323 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 324 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 325 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 326 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 327 | LD_RUNPATH_SEARCH_PATHS = ( 328 | "$(inherited)", 329 | "@executable_path/Frameworks", 330 | ); 331 | MARKETING_VERSION = 1.0; 332 | PRODUCT_BUNDLE_IDENTIFIER = me.apurin.Examples; 333 | PRODUCT_NAME = "$(TARGET_NAME)"; 334 | SWIFT_EMIT_LOC_STRINGS = YES; 335 | SWIFT_VERSION = 5.0; 336 | TARGETED_DEVICE_FAMILY = "1,2"; 337 | }; 338 | name = Debug; 339 | }; 340 | 61EA31B5275A247600AF136E /* Release */ = { 341 | isa = XCBuildConfiguration; 342 | buildSettings = { 343 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 344 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 345 | CODE_SIGN_STYLE = Automatic; 346 | CURRENT_PROJECT_VERSION = 1; 347 | DEVELOPMENT_ASSET_PATHS = ""; 348 | DEVELOPMENT_TEAM = FK4VRNJVL7; 349 | ENABLE_PREVIEWS = YES; 350 | GENERATE_INFOPLIST_FILE = YES; 351 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 352 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 353 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 354 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 355 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 356 | LD_RUNPATH_SEARCH_PATHS = ( 357 | "$(inherited)", 358 | "@executable_path/Frameworks", 359 | ); 360 | MARKETING_VERSION = 1.0; 361 | PRODUCT_BUNDLE_IDENTIFIER = me.apurin.Examples; 362 | PRODUCT_NAME = "$(TARGET_NAME)"; 363 | SWIFT_EMIT_LOC_STRINGS = YES; 364 | SWIFT_VERSION = 5.0; 365 | TARGETED_DEVICE_FAMILY = "1,2"; 366 | }; 367 | name = Release; 368 | }; 369 | /* End XCBuildConfiguration section */ 370 | 371 | /* Begin XCConfigurationList section */ 372 | 61EA31A0275A247500AF136E /* Build configuration list for PBXProject "Examples" */ = { 373 | isa = XCConfigurationList; 374 | buildConfigurations = ( 375 | 61EA31B1275A247600AF136E /* Debug */, 376 | 61EA31B2275A247600AF136E /* Release */, 377 | ); 378 | defaultConfigurationIsVisible = 0; 379 | defaultConfigurationName = Release; 380 | }; 381 | 61EA31B3275A247600AF136E /* Build configuration list for PBXNativeTarget "Examples" */ = { 382 | isa = XCConfigurationList; 383 | buildConfigurations = ( 384 | 61EA31B4275A247600AF136E /* Debug */, 385 | 61EA31B5275A247600AF136E /* Release */, 386 | ); 387 | defaultConfigurationIsVisible = 0; 388 | defaultConfigurationName = Release; 389 | }; 390 | /* End XCConfigurationList section */ 391 | 392 | /* Begin XCSwiftPackageProductDependency section */ 393 | A351EA8927683F98003129AC /* RepresentableKit */ = { 394 | isa = XCSwiftPackageProductDependency; 395 | productName = RepresentableKit; 396 | }; 397 | /* End XCSwiftPackageProductDependency section */ 398 | }; 399 | rootObject = 61EA319D275A247500AF136E /* Project object */; 400 | } 401 | -------------------------------------------------------------------------------- /Examples/Examples.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Examples/Sources/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Examples/Sources/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Examples/Sources/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Examples/Sources/ExamplesApp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ExamplesApp.swift 3 | // Examples 4 | // 5 | // Created by Mikhail Apurin on 2021/12/03. 6 | // 7 | 8 | import SwiftUI 9 | 10 | @main 11 | struct ExamplesApp: App { 12 | var body: some Scene { 13 | WindowGroup { 14 | Text("For sample code see previews inside the Previews folder.") 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Examples/Sources/Previews/CustomUIView+Previews.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomUIView+Previews.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/26. 6 | // 7 | 8 | import SwiftUI 9 | import RepresentableKit 10 | 11 | // A simple custom UIView that is laid out with Auto Layout and does not customize 12 | // its intrinsic content size (which defaults to UIView.noIntrinsicMetric). 13 | 14 | struct CustomUIView_Previews: PreviewProvider { 15 | static var previews: some View { 16 | Group { 17 | UIViewAdaptor { 18 | CustomUIView() 19 | } 20 | .frame(width: 160) 21 | .fixedSize() 22 | .border(Color.orange, width: 1) 23 | .padding() 24 | .previewLayout(.sizeThatFits) 25 | .previewDisplayName("Size that fits: (W 160 H ideal)") 26 | 27 | ScrollView { 28 | ForEach(0...10, id: \.self) { _ in 29 | VStack { 30 | HStack { 31 | UIViewAdaptor { 32 | CustomUIView() 33 | } 34 | .border(.orange, width: 1) 35 | 36 | UIViewAdaptor { 37 | CustomUIView() 38 | } 39 | .border(.orange, width: 1) 40 | } 41 | 42 | VStack { 43 | UIViewAdaptor { 44 | CustomUIView() 45 | } 46 | .border(.orange, width: 1) 47 | 48 | UIViewAdaptor { 49 | CustomUIView() 50 | } 51 | .border(.orange, width: 1) 52 | } 53 | } 54 | } 55 | .padding() 56 | } 57 | } 58 | .previewDisplayName("Fit view in mixed stacks") 59 | } 60 | } 61 | 62 | /// A simple custom UIView with an image, a main label and a secondary label. 63 | final class CustomUIView: UIView { 64 | private(set) lazy var imageView = makeImageView( 65 | systemName: "photo.on.rectangle.angled" 66 | ) 67 | 68 | private(set) lazy var mainLabel = makeLabel( 69 | text: "Lorem ipsum dolor sit amet", 70 | font: .preferredFont(forTextStyle: .body), 71 | color: .label 72 | ) 73 | 74 | private(set) lazy var detailLabel = makeLabel( 75 | text: "Consectetur adipiscing elit. Integer sed dui at risus porta venenatis in sed ante.", 76 | font: .preferredFont(forTextStyle: .caption2), 77 | color: .secondaryLabel 78 | ) 79 | 80 | private func makeImageView(systemName: String) -> UIImageView { 81 | let view = UIImageView(image: UIImage(systemName: systemName)) 82 | view.preferredSymbolConfiguration = .init(textStyle: .title1) 83 | view.contentMode = .scaleAspectFit 84 | view.tintColor = UIColor.systemGray 85 | return view 86 | } 87 | 88 | private func makeLabel(text: String, font: UIFont, color: UIColor) -> UILabel { 89 | let view = UILabel() 90 | view.numberOfLines = 0 91 | view.text = text 92 | view.font = font 93 | view.textColor = color 94 | return view 95 | } 96 | 97 | init() { 98 | super.init(frame: .zero) 99 | setConstraints() 100 | } 101 | 102 | required init?(coder: NSCoder) { 103 | fatalError("init(coder:) has not been implemented") 104 | } 105 | 106 | private func setConstraints() { 107 | layoutMargins = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16) 108 | 109 | [imageView, mainLabel, detailLabel].forEach { 110 | addSubview($0) 111 | $0.translatesAutoresizingMaskIntoConstraints = false 112 | } 113 | imageView.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 751), for: .horizontal) 114 | imageView.setContentHuggingPriority(UILayoutPriority(rawValue: 751), for: .horizontal) 115 | NSLayoutConstraint.activate([ 116 | imageView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor), 117 | imageView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor), 118 | imageView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor), 119 | 120 | mainLabel.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 8), 121 | mainLabel.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor), 122 | mainLabel.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor), 123 | 124 | detailLabel.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 8), 125 | detailLabel.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor), 126 | detailLabel.topAnchor.constraint(equalTo: mainLabel.bottomAnchor, constant: 4), 127 | detailLabel.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor), 128 | ]) 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /Examples/Sources/Previews/UIImageView+Previews.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIImageView+Previews.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/26. 6 | // 7 | 8 | import SwiftUI 9 | import RepresentableKit 10 | 11 | // UIImageView is notable as a system component that has a specified 12 | // intrinsic content size that is equal to its UIImage dimensions. 13 | 14 | struct UIImageView_Previews: PreviewProvider { 15 | static var previews: some View { 16 | Group { 17 | UIViewAdaptor { 18 | makeView() 19 | } 20 | .aspectRatio(contentMode: .fit) 21 | .padding() 22 | .frame(width: 100) 23 | .previewLayout(.sizeThatFits) 24 | .previewDisplayName("Size that fits: (W 100 H ideal) / original aspect ratio") 25 | 26 | ScrollView { 27 | ForEach(0...10, id: \.self) { _ in 28 | HStack { 29 | UIViewAdaptor { 30 | makeView() 31 | } 32 | .aspectRatio(nil, contentMode: .fit) 33 | 34 | UIViewAdaptor { 35 | makeView() 36 | } 37 | .aspectRatio(nil, contentMode: .fit) 38 | } 39 | } 40 | .padding() 41 | } 42 | .previewDisplayName("Fit view in HStacks with original aspect ratio") 43 | } 44 | } 45 | 46 | static func makeView() -> UIImageView { 47 | let view = UIImageView() 48 | view.image = UIImage(systemName: "heart") 49 | return view 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Examples/Sources/Previews/UILabel+Previews.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UILabel+Previews.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/26. 6 | // 7 | 8 | import SwiftUI 9 | import RepresentableKit 10 | 11 | // UILabel is notable as a system component that has a specified intrinsic 12 | // content size which reflects its text content. By default, width/height 13 | // is calculated without any bounds, but you can set `preferredMaxLayoutWidth` 14 | // to non-zero value as the upper bound for width. 15 | 16 | let lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim congue justo ac dignissim. Integer eget semper mi. In non vestibulum risus." 17 | 18 | struct UILabel_Previews: PreviewProvider { 19 | static var previews: some View { 20 | Group { 21 | UIViewAdaptor { 22 | makeMultilineLabel(text: "Some text") 23 | } 24 | .previewDisplayName("Size that fits: (W ideal H ideal)") 25 | 26 | UIViewAdaptor { 27 | makeMultilineLabel(text: lipsum) 28 | } 29 | .frame(width: 390) 30 | .previewDisplayName("Size that fits: (W 390 H ideal)") 31 | 32 | UIViewAdaptor { 33 | makeMultilineLabel(text: lipsum) 34 | } 35 | .frame(idealWidth: 390, idealHeight: 44) 36 | .previewDisplayName("Size that fits: (W 390 H 44)") 37 | } 38 | .border(Color.red) 39 | .padding() 40 | .fixedSize() 41 | .previewLayout(.sizeThatFits) 42 | 43 | Group { 44 | UILabelSizeAdjustView(text: lipsum) 45 | .previewDisplayName("Interactively fit view in specified width") 46 | } 47 | } 48 | 49 | static func makeMultilineLabel(text: String) -> UILabel { 50 | let view = UILabel() 51 | view.numberOfLines = 0 52 | view.text = text 53 | return view 54 | } 55 | } 56 | 57 | struct UILabelSizeAdjustView: View { 58 | let text: String 59 | 60 | @State var width: Double = 200 61 | 62 | @State var currentSize: CGSize = .zero 63 | 64 | var body: some View { 65 | SizeAdjustView { 66 | UIViewAdaptor { 67 | let view = UILabel() 68 | view.numberOfLines = 0 69 | view.text = text 70 | return view 71 | } 72 | .frame(width: width) 73 | .fixedSize() 74 | .border(Color.orange) 75 | } controls: { 76 | Slider(value: $width, in: 0...currentSize.width) 77 | .padding(.horizontal) 78 | } 79 | .inspectSize($currentSize) 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Examples/Sources/Previews/UISlider+Previews.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UISlider+Previews.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/26. 6 | // 7 | 8 | import SwiftUI 9 | import RepresentableKit 10 | 11 | // UISlider is notable as a system component that has a specified 12 | // intrinsic content height, but not width 13 | 14 | struct UISlider_Previews: PreviewProvider { 15 | static var previews: some View { 16 | Group { 17 | UIViewAdaptor { 18 | UISlider() 19 | } 20 | .fixedSize(horizontal: false, vertical: true) 21 | .padding() 22 | .frame(width: 150) 23 | .previewLayout(.sizeThatFits) 24 | .previewDisplayName("Size that fits: (W 150 H ideal)") 25 | 26 | UISliderSizeAdjustView() 27 | .previewDisplayName("Synchronized UISlider + SwiftUI Slider") 28 | } 29 | } 30 | } 31 | 32 | struct UISliderSizeAdjustView: View { 33 | let min: CGFloat = -1 34 | 35 | let max: CGFloat = 1 36 | 37 | @State var value: CGFloat = 0 38 | 39 | var body: some View { 40 | SizeAdjustView { 41 | ForEach(0...10, id: \.self) { _ in 42 | HStack(spacing: 16) { 43 | UIViewAdaptor { 44 | // Using a custom UIViewRepresentable that is defined below 45 | UISliderView(min: min, max: max, value: $value) 46 | } 47 | 48 | UIViewAdaptor { 49 | // Using a custom UIViewRepresentable that is defined below 50 | UISliderView(min: min, max: max, value: $value) 51 | } 52 | } 53 | } 54 | .accentColor(.red) 55 | .padding() 56 | } controls: { 57 | Slider(value: $value, in: min...max) 58 | .padding(.horizontal) 59 | } 60 | } 61 | } 62 | 63 | /// A custom UISlider representable to enable interacting with the view. 64 | struct UISliderView: UIViewRepresentable { 65 | let min: CGFloat 66 | 67 | let max: CGFloat 68 | 69 | @Binding var value: CGFloat 70 | 71 | final class Coordinator { 72 | var control: UISliderView 73 | 74 | init(_ control: UISliderView) { 75 | self.control = control 76 | } 77 | 78 | @objc func updateValue(sender: UISlider) { 79 | control.value = CGFloat(sender.value) 80 | } 81 | } 82 | 83 | func makeCoordinator() -> Coordinator { 84 | Coordinator(self) 85 | } 86 | 87 | func makeUIView(context: Context) -> UISlider { 88 | let uiView = UISlider() 89 | uiView.addTarget(context.coordinator, action: #selector(Coordinator.updateValue(sender:)), for: .valueChanged) 90 | return uiView 91 | } 92 | 93 | func updateUIView(_ uiView: UISlider, context: Context) { 94 | uiView.minimumValue = Float(min) 95 | uiView.maximumValue = Float(max) 96 | uiView.value = Float(value) 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Examples/Sources/Utility/SizeAdjustView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SizeAdjustView.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/27. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct SizeAdjustView: View { 11 | let canvas: Canvas 12 | 13 | let controls: Controls 14 | 15 | init(@ViewBuilder canvas: () -> Canvas, @ViewBuilder controls: () -> Controls) { 16 | self.canvas = canvas() 17 | self.controls = controls() 18 | } 19 | 20 | var body: some View { 21 | VStack(spacing: .zero) { 22 | ScrollView { 23 | canvas 24 | .frame(maxWidth: .infinity, maxHeight: .infinity) 25 | } 26 | 27 | Divider() 28 | 29 | controls 30 | .padding(.vertical) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Examples/Sources/Utility/SizeGetter.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SizeGetter.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/26. 6 | // 7 | 8 | import SwiftUI 9 | import RepresentableKit 10 | 11 | struct SizeGetter: ViewModifier { 12 | struct SizeKey: PreferenceKey { 13 | static var defaultValue: CGSize { .zero } 14 | 15 | static func reduce(value: inout CGSize, nextValue: () -> CGSize) { 16 | value = nextValue() 17 | } 18 | } 19 | 20 | @Binding var size: CGSize 21 | 22 | func body(content: Content) -> some View { 23 | content 24 | .background(GeometryReader { proxy in 25 | Color.clear 26 | .preference(key: SizeKey.self, value: proxy.size) 27 | .onPreferenceChange(SizeKey.self) { size = $0 } 28 | }) 29 | } 30 | } 31 | 32 | extension View { 33 | func inspectSize(_ size: Binding) -> some View { 34 | self 35 | .modifier(SizeGetter(size: size)) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Mike Apurin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.5 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "RepresentableKit", 8 | platforms: [.iOS(.v11)], 9 | products: [ 10 | .library( 11 | name: "RepresentableKit", 12 | targets: ["RepresentableKit"] 13 | ), 14 | ], 15 | targets: [ 16 | .target( 17 | name: "RepresentableKit" 18 | ) 19 | ] 20 | ) 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RepresentableKit 2 | 3 | Apple provides us two ways to use UIKit views in SwiftUI: 4 | 5 | 1. [`UIViewRepresentable`](https://developer.apple.com/documentation/swiftui/uiviewrepresentable) 6 | 1. [`UIViewControllerRepresentable`](https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable) 7 | 8 | The way those Views interact with the layout system are not really well-documented and can be a pain to implement correctly. 9 | 10 | RepresentableKit 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. 11 | 12 | A minimal usage example would be previewing a simple `UILabel` with multiple lines of text. 13 | 14 | ```swift 15 | import SwiftUI 16 | import RepresentableKit 17 | 18 | struct UILabel_Previews: PreviewProvider { 19 | static var previews: some View { 20 | UIViewAdaptor { 21 | let view = UILabel() 22 | view.numberOfLines = 0 23 | 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." 24 | return view 25 | } 26 | .padding() 27 | } 28 | } 29 | ``` 30 | 31 | Note that had we chosen to write this naively with `UIViewRepresentable` without further customizing layout, it would render as a single line of text. 32 | 33 | For further examples, see the provided [`Examples`](Examples/) project. 34 | 35 | # Installation 36 | 37 | Use Swift Package Manager to install RepresentableKit. 38 | 39 | - Use Xcode and add this repository as a dependency. 40 | - Alternatively, add this repository as a `dependency` to your `Package.swift`. 41 | 42 | ```swift 43 | dependencies: [ 44 | .package(url: "https://github.com/yumemi-inc/RepresentableKit.git", .upToNextMajor(from: "0.1.0")) 45 | ] 46 | ``` 47 | 48 | 49 | # UIViewRepresentable 50 | 51 | Let's look at how `UIViewRepresentable` chooses an appropriate size for the contained UIView. 52 | 53 | First 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)). 54 | 55 | 1. Parent proposes a size for child 56 | 1. Child chooses its own size 57 | 1. Parent places child in parent's coordinate space 58 | 59 | The 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). 60 | 61 | `UIViewRepresentable` uses Auto Layout intrinsic content size and layout priorities to map the contained UIView size. 62 | 63 | | [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 | 64 | |---|---|---|---| 65 | | (-1) | - | - | `0 … 0 … ∞` | 66 | | x | < 750 | < 750 | `0 … x … ∞` | 67 | | x | < 750 | >= 750 | `0 … x … x` | 68 | | x | >= 750 | < 750 | `x … x … ∞` | 69 | | x | >= 750 | >= 750 | `x … x … x` | 70 | 71 | - The result size is shown as `min … ideal … max` 72 | - `-1` = [`UIView.noIntrinsicMetric`](https://developer.apple.com/documentation/uikit/uiview/1622486-nointrinsicmetric/) 73 | - `750` = [`UILayoutPriority.defaultHigh`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622249-defaulthigh) 74 | 75 | `UIViewRepresentable` will also listen to [invalidateIntrinsicContentSize()](https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize/) to update its size. 76 | 77 | Using 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. 78 | 79 | Most 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. 80 | 81 | RepresentableKit abstracts over those concepts in two ways: 82 | 83 | 1. [UIViewFlexibility](Sources/RepresentableKit/UIViewFlexibility.swift) to control min/max sizes via the layout priorities. 84 | 1. [UIViewIdealSizeCalculator](Sources/RepresentableKit/UIViewIdealSize.swift) to control the ideal size. 85 | 86 | You can specify a custom sizing behavior by modifying [`UIViewAdaptor.init`](Sources/RepresentableKit/UIViewAdaptor.swift) attributes. 87 | 88 | # UIViewControllerRepresentable 89 | 90 | For now, RepresentableKit does not provide any compatibility with `UIViewControllerRepresentable`. 91 | -------------------------------------------------------------------------------- /Sources/RepresentableKit/UIViewAdaptor.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewAdaptor.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/26. 6 | // 7 | 8 | #if os(iOS) 9 | import SwiftUI 10 | 11 | /// Adapt UIKit views to be used in SwiftUI. 12 | @available(iOS 13, *) 13 | public struct UIViewAdaptor: View where Representable.UIViewType == Content { 14 | typealias Holder = UIViewHolder 15 | 16 | struct SizeKey: PreferenceKey { 17 | public static var defaultValue: CGSize { .zero } 18 | 19 | public static func reduce(value: inout CGSize, nextValue: () -> CGSize) { 20 | value = nextValue() 21 | } 22 | } 23 | 24 | @State private var holder: Holder 25 | 26 | @State private var idealSize: IdealSize? 27 | 28 | let representableFactory: () -> Representable 29 | 30 | /// - Parameters: 31 | /// - flexibility: View dimensions that can be compressed/stretched. 32 | /// - idealSizeCalculator: Calculation of the ideal size that fits the current size of the view. 33 | /// - view: `UIView` factory. 34 | /// - representable: Factory method that creates `UIViewRepresentable` appropriate for the view. 35 | public init( 36 | flexibility: UIViewFlexibility = .all, 37 | idealSizeCalculator: UIViewIdealSizeCalculator = .default, 38 | representable: @escaping () -> Representable 39 | ) { 40 | self._holder = .init( 41 | initialValue: UIViewHolder(flexibility: flexibility, idealSizeCalculator: idealSizeCalculator) 42 | ) 43 | self.representableFactory = representable 44 | } 45 | 46 | public var body: some View { 47 | RepresentableWrapper(wrap: representableFactory(), holder: holder) 48 | .background(GeometryReader { proxy in 49 | Color.clear 50 | .preference(key: SizeKey.self, value: proxy.size) 51 | .onPreferenceChange(SizeKey.self) { idealSize = holder.idealSize(proxySize: $0) } 52 | }) 53 | .frame( 54 | idealWidth: idealSize?.width, 55 | idealHeight: idealSize?.height 56 | ) 57 | } 58 | } 59 | 60 | @available(iOS 13, *) 61 | public extension UIViewAdaptor where Representable == BasicUIViewRepresentable { 62 | init( 63 | flexibility: UIViewFlexibility = .all, 64 | idealSizeCalculator: UIViewIdealSizeCalculator = .default, 65 | makeUIView: @escaping () -> Content 66 | ) { 67 | self.init( 68 | flexibility: flexibility, 69 | idealSizeCalculator: idealSizeCalculator, 70 | representable: { BasicUIViewRepresentable(makeUIView: makeUIView) } 71 | ) 72 | } 73 | } 74 | 75 | // MARK: - 76 | 77 | /// Class responsible for setting the view flexibility and updating the ideal size. 78 | @available(iOS 13, *) 79 | struct UIViewHolder { 80 | /// Storage for the view. To avoid holding on to the instance after SwiftUI has already discarded the view, this storage is weak. 81 | final class ViewStorage { 82 | weak var view: Content? 83 | } 84 | 85 | private let storage = ViewStorage() 86 | 87 | let flexibility: UIViewFlexibility 88 | 89 | let idealSizeCalculator: UIViewIdealSizeCalculator 90 | 91 | func updateView(_ uiView: Content) { 92 | storage.view = uiView 93 | uiView.apply(flexibility: flexibility) 94 | } 95 | 96 | func idealSize(proxySize: CGSize) -> IdealSize? { 97 | guard let uiView = storage.view else { return nil } 98 | return idealSizeCalculator.viewIdealSizeInSize(uiView, proxySize) 99 | } 100 | 101 | #if DEBUG 102 | private struct ProposedSize { 103 | let width: CGFloat? 104 | let height: CGFloat? 105 | } 106 | 107 | // ⚠️: This function uses private SwiftUI API. Make sure to use it only for debug purposes and never in the actual release build of the app. 108 | // Override size calculation for UIViewRepresentable to calculate the correct ideal size for the view on the first go. 109 | // This is a workaround for Xcode Previews limitation for .sizeThatFits preview layout of views that are set to .fixedSize(). 110 | // Since we are using a GeometryReader to update ideal size, it will take several layout passes for the view become the correct size. 111 | // Xcode Previews, however, will take the size reported in the first layout pass, even though they show the actual view laid out correctly. 112 | func overrideSizeThatFits(_ size: inout CGSize, in proposedSize: SwiftUI._ProposedSize, uiView: Content) { 113 | let proposedSize = unsafeBitCast(proposedSize, to: ProposedSize.self) 114 | 115 | let targetSize = CGSize( 116 | width: proposedSize.width ?? UIView.layoutFittingCompressedSize.width, 117 | height: proposedSize.height ?? UIView.layoutFittingCompressedSize.height 118 | ) 119 | 120 | let idealSize = idealSizeCalculator.viewIdealSizeInSize(uiView, targetSize) 121 | 122 | if proposedSize.width == nil, let idealWidth = idealSize.width { 123 | size.width = idealWidth 124 | } 125 | 126 | if proposedSize.height == nil, let idealHeight = idealSize.height { 127 | size.height = idealHeight 128 | } 129 | } 130 | #endif 131 | } 132 | 133 | // MARK: - 134 | 135 | /// A basic representable to be used in cases where the view does not need any customization or interactivity after instantiation. 136 | @available(iOS 13, *) 137 | public struct BasicUIViewRepresentable: UIViewRepresentable { 138 | public typealias UIViewType = Content 139 | 140 | public let makeUIView: () -> Content 141 | 142 | public func makeUIView(context: Context) -> UIViewType { 143 | makeUIView() 144 | } 145 | 146 | public func updateUIView(_ uiView: UIViewType, context: Context) { } 147 | } 148 | 149 | /// UIViewRepresentable wrapper that passes its view to the outside view holder 150 | @available(iOS 13, *) 151 | public struct RepresentableWrapper: UIViewRepresentable { 152 | public typealias UIViewType = Wrapped.UIViewType 153 | 154 | public typealias Coordinator = Wrapped.Coordinator 155 | 156 | let wrap: Wrapped 157 | 158 | let holder: UIViewHolder 159 | 160 | public func makeCoordinator() -> Wrapped.Coordinator { 161 | wrap.makeCoordinator() 162 | } 163 | 164 | public func makeUIView(context: Context) -> Wrapped.UIViewType { 165 | let uiView = wrap.makeUIView(context: unsafeBitCast(context, to: Wrapped.Context.self)) 166 | holder.updateView(uiView) 167 | return uiView 168 | } 169 | 170 | public func updateUIView(_ uiView: Wrapped.UIViewType, context: Context) { 171 | wrap.updateUIView(uiView, context: unsafeBitCast(context, to: Wrapped.Context.self)) 172 | } 173 | 174 | public static func dismantleUIView(_ uiView: Wrapped.UIViewType, coordinator: Wrapped.Coordinator) { 175 | Wrapped.dismantleUIView(uiView, coordinator: coordinator) 176 | } 177 | 178 | // ⚠️: This is private SwiftUI API. Make sure to use it only for debug purposes and never in the actual release build of the app. 179 | #if DEBUG 180 | public func _overrideSizeThatFits(_ size: inout CGSize, in proposedSize: SwiftUI._ProposedSize, uiView: UIViewType) { 181 | holder.overrideSizeThatFits(&size, in: proposedSize, uiView: uiView) 182 | } 183 | #endif 184 | } 185 | #endif 186 | -------------------------------------------------------------------------------- /Sources/RepresentableKit/UIViewFlexibility.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewFlexibility.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/27. 6 | // 7 | 8 | #if os(iOS) 9 | import UIKit 10 | 11 | /// Flexibility of a view is its ability to take a size that is less or more than its ideal size in a dimension. 12 | @available(iOS 13, *) 13 | public struct UIViewFlexibility: OptionSet { 14 | public let rawValue: Int8 15 | 16 | public init(rawValue: Int8) { 17 | self.rawValue = rawValue 18 | } 19 | 20 | /// View width can be compressed to any value in 0...idealWidth. 21 | public static let minHorizontal = UIViewFlexibility(rawValue: 1 << 0) 22 | 23 | /// View width can be stretched to any value in idealWidth...infinity. 24 | public static let maxHorizontal = UIViewFlexibility(rawValue: 1 << 1) 25 | 26 | /// View height can be compressed to any value in 0...idealHeight. 27 | public static let minVertical = UIViewFlexibility(rawValue: 1 << 2) 28 | 29 | /// View height can be stretched to any value in idealHeight...infinity. 30 | public static let maxVertical = UIViewFlexibility(rawValue: 1 << 3) 31 | 32 | /// View width can be compressed and stretched to any value in 0...infinity. 33 | public static let horizontal: UIViewFlexibility = [.minHorizontal, .maxHorizontal] 34 | 35 | /// View height can be compressed and stretched to any value in 0...infinity. 36 | public static let vertical: UIViewFlexibility = [.minVertical, .maxVertical] 37 | 38 | /// View width and height can be compressed and stretched to any value in 0...infinity. 39 | public static let all: UIViewFlexibility = [.horizontal, .vertical] 40 | 41 | /// View width and height must maintain their ideal size and can not be stretched or compressed. 42 | public static let none: UIViewFlexibility = [] 43 | 44 | /// SwiftUI picks up UIView flexibility from its compression resistance and content hugging priorities. 45 | /// A value of .defaultHigh (750) and above signifies that the view is not flexible for this dimension and should maintain its ideal size. 46 | public func priority(_ value: UIViewFlexibility) -> UILayoutPriority { 47 | contains(value) ? .defaultLow : .defaultHigh 48 | } 49 | } 50 | 51 | @available(iOS 13, *) 52 | public extension UIView { 53 | /// Set compression resistance and content hugging priorities so that SwiftUI can pick up the flexibility of this view. 54 | func apply(flexibility: UIViewFlexibility) { 55 | setContentCompressionResistancePriority(flexibility.priority(.minHorizontal), for: .horizontal) 56 | setContentCompressionResistancePriority(flexibility.priority(.minVertical), for: .vertical) 57 | setContentHuggingPriority(flexibility.priority(.maxHorizontal), for: .horizontal) 58 | setContentHuggingPriority(flexibility.priority(.maxVertical), for: .vertical) 59 | } 60 | } 61 | #endif 62 | -------------------------------------------------------------------------------- /Sources/RepresentableKit/UIViewIdealSize.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewIdealSize.swift 3 | // 4 | // 5 | // Created by Mikhail Apurin on 2021/10/27. 6 | // 7 | 8 | #if os(iOS) 9 | import UIKit 10 | 11 | /// Ideal size for the view. In Auto Layout terms, this is equivalent to the view intrinsic content size. 12 | @available(iOS 13, *) 13 | public struct IdealSize: Hashable { 14 | /// Ideal width. When nil, the width of view's intrinsic content size will be used. 15 | public let width: CGFloat? 16 | 17 | /// Ideal height. When nil, the height of view's intrinsic content size will be used. 18 | public let height: CGFloat? 19 | 20 | public init(width: CGFloat?, height: CGFloat?) { 21 | self.width = width 22 | self.height = height 23 | } 24 | } 25 | 26 | /// Calculation of the ideal size that fits the current size of the view. 27 | @available(iOS 13, *) 28 | public struct UIViewIdealSizeCalculator { 29 | /// Calculate the view ideal size fot a given size. 30 | public var viewIdealSizeInSize: (Content, CGSize) -> IdealSize 31 | 32 | public init(viewIdealSizeInSize: @escaping (Content, CGSize) -> IdealSize) { 33 | self.viewIdealSizeInSize = viewIdealSizeInSize 34 | } 35 | } 36 | 37 | @available(iOS 13, *) 38 | public extension UIViewIdealSizeCalculator { 39 | /// Set the ideal size so that ideal width fits inside the proposed frame width. Ideal height is appropriate for the fitted width, but can exceed the proposed height. 40 | static var `default`: Self { 41 | .init { content, size in 42 | let fittingSize = content.systemLayoutSizeFitting( 43 | size, 44 | withHorizontalFittingPriority: .required, 45 | verticalFittingPriority: .fittingSizeLevel 46 | ) 47 | return IdealSize(width: fittingSize.width, height: fittingSize.height) 48 | } 49 | } 50 | 51 | /// Do not override the ideal size. Use intrinsic content size of the view. 52 | static var none: Self { 53 | .init { _, _ in 54 | IdealSize(width: nil, height: nil) 55 | } 56 | } 57 | } 58 | #endif 59 | --------------------------------------------------------------------------------