├── .gitignore ├── CenteredCollectionView-Sample ├── CenteredCollectionView-Sample.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── CenteredCollectionView-Sample │ ├── AppDelegate.swift │ ├── Common │ ├── Roundable.swift │ ├── RoundedCollectionViewCell.swift │ └── RoundedView.swift │ ├── Resources │ ├── Assets.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ └── Info.plist │ ├── Roundable.swift │ ├── RoundedCollectionViewCell.swift │ ├── Storyboards │ └── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ └── ViewController.swift ├── CollectionViewCenterLayout.swift ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 653F100E208685BC003D2635 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653F100D208685BC003D2635 /* AppDelegate.swift */; }; 11 | 653F1010208685BC003D2635 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653F100F208685BC003D2635 /* ViewController.swift */; }; 12 | 653F1013208685BC003D2635 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 653F1011208685BC003D2635 /* Main.storyboard */; }; 13 | 653F1015208685BC003D2635 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 653F1014208685BC003D2635 /* Assets.xcassets */; }; 14 | 653F1018208685BC003D2635 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 653F1016208685BC003D2635 /* LaunchScreen.storyboard */; }; 15 | 653F102720868C31003D2635 /* Roundable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653F102420868C31003D2635 /* Roundable.swift */; }; 16 | 653F102820868C31003D2635 /* RoundedCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653F102520868C31003D2635 /* RoundedCollectionViewCell.swift */; }; 17 | 653F102920868C31003D2635 /* RoundedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653F102620868C31003D2635 /* RoundedView.swift */; }; 18 | 65453A0420A4279800248B57 /* CollectionViewCenterLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65453A0320A4279800248B57 /* CollectionViewCenterLayout.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXFileReference section */ 22 | 653F100A208685BC003D2635 /* CenteredCollectionView-Sample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "CenteredCollectionView-Sample.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 23 | 653F100D208685BC003D2635 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 24 | 653F100F208685BC003D2635 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 25 | 653F1012208685BC003D2635 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 26 | 653F1014208685BC003D2635 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 27 | 653F1017208685BC003D2635 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 28 | 653F1019208685BC003D2635 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 29 | 653F102420868C31003D2635 /* Roundable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Roundable.swift; sourceTree = ""; }; 30 | 653F102520868C31003D2635 /* RoundedCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoundedCollectionViewCell.swift; sourceTree = ""; }; 31 | 653F102620868C31003D2635 /* RoundedView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoundedView.swift; sourceTree = ""; }; 32 | 65453A0320A4279800248B57 /* CollectionViewCenterLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CollectionViewCenterLayout.swift; path = ../../CollectionViewCenterLayout.swift; sourceTree = ""; }; 33 | /* End PBXFileReference section */ 34 | 35 | /* Begin PBXFrameworksBuildPhase section */ 36 | 653F1007208685BC003D2635 /* Frameworks */ = { 37 | isa = PBXFrameworksBuildPhase; 38 | buildActionMask = 2147483647; 39 | files = ( 40 | ); 41 | runOnlyForDeploymentPostprocessing = 0; 42 | }; 43 | /* End PBXFrameworksBuildPhase section */ 44 | 45 | /* Begin PBXGroup section */ 46 | 653F1001208685BC003D2635 = { 47 | isa = PBXGroup; 48 | children = ( 49 | 653F100C208685BC003D2635 /* CenteredCollectionView-Sample */, 50 | 653F100B208685BC003D2635 /* Products */, 51 | ); 52 | sourceTree = ""; 53 | }; 54 | 653F100B208685BC003D2635 /* Products */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | 653F100A208685BC003D2635 /* CenteredCollectionView-Sample.app */, 58 | ); 59 | name = Products; 60 | sourceTree = ""; 61 | }; 62 | 653F100C208685BC003D2635 /* CenteredCollectionView-Sample */ = { 63 | isa = PBXGroup; 64 | children = ( 65 | 653F102320868C1A003D2635 /* Common */, 66 | 653F100D208685BC003D2635 /* AppDelegate.swift */, 67 | 653F100F208685BC003D2635 /* ViewController.swift */, 68 | 65453A0320A4279800248B57 /* CollectionViewCenterLayout.swift */, 69 | 653F102220868BEA003D2635 /* Storyboards */, 70 | 653F102120868BDA003D2635 /* Resources */, 71 | ); 72 | path = "CenteredCollectionView-Sample"; 73 | sourceTree = ""; 74 | }; 75 | 653F102120868BDA003D2635 /* Resources */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 653F1019208685BC003D2635 /* Info.plist */, 79 | 653F1014208685BC003D2635 /* Assets.xcassets */, 80 | ); 81 | path = Resources; 82 | sourceTree = ""; 83 | }; 84 | 653F102220868BEA003D2635 /* Storyboards */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | 653F1016208685BC003D2635 /* LaunchScreen.storyboard */, 88 | 653F1011208685BC003D2635 /* Main.storyboard */, 89 | ); 90 | path = Storyboards; 91 | sourceTree = ""; 92 | }; 93 | 653F102320868C1A003D2635 /* Common */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 653F102420868C31003D2635 /* Roundable.swift */, 97 | 653F102620868C31003D2635 /* RoundedView.swift */, 98 | 653F102520868C31003D2635 /* RoundedCollectionViewCell.swift */, 99 | ); 100 | path = Common; 101 | sourceTree = ""; 102 | }; 103 | /* End PBXGroup section */ 104 | 105 | /* Begin PBXNativeTarget section */ 106 | 653F1009208685BC003D2635 /* CenteredCollectionView-Sample */ = { 107 | isa = PBXNativeTarget; 108 | buildConfigurationList = 653F101C208685BC003D2635 /* Build configuration list for PBXNativeTarget "CenteredCollectionView-Sample" */; 109 | buildPhases = ( 110 | 653F1006208685BC003D2635 /* Sources */, 111 | 653F1007208685BC003D2635 /* Frameworks */, 112 | 653F1008208685BC003D2635 /* Resources */, 113 | ); 114 | buildRules = ( 115 | ); 116 | dependencies = ( 117 | ); 118 | name = "CenteredCollectionView-Sample"; 119 | productName = "CenteredCollectionView-Sample"; 120 | productReference = 653F100A208685BC003D2635 /* CenteredCollectionView-Sample.app */; 121 | productType = "com.apple.product-type.application"; 122 | }; 123 | /* End PBXNativeTarget section */ 124 | 125 | /* Begin PBXProject section */ 126 | 653F1002208685BC003D2635 /* Project object */ = { 127 | isa = PBXProject; 128 | attributes = { 129 | LastSwiftUpdateCheck = 0920; 130 | LastUpgradeCheck = 0920; 131 | ORGANIZATIONNAME = "Dejan Skledar"; 132 | TargetAttributes = { 133 | 653F1009208685BC003D2635 = { 134 | CreatedOnToolsVersion = 9.2; 135 | ProvisioningStyle = Automatic; 136 | }; 137 | }; 138 | }; 139 | buildConfigurationList = 653F1005208685BC003D2635 /* Build configuration list for PBXProject "CenteredCollectionView-Sample" */; 140 | compatibilityVersion = "Xcode 8.0"; 141 | developmentRegion = en; 142 | hasScannedForEncodings = 0; 143 | knownRegions = ( 144 | en, 145 | Base, 146 | ); 147 | mainGroup = 653F1001208685BC003D2635; 148 | productRefGroup = 653F100B208685BC003D2635 /* Products */; 149 | projectDirPath = ""; 150 | projectRoot = ""; 151 | targets = ( 152 | 653F1009208685BC003D2635 /* CenteredCollectionView-Sample */, 153 | ); 154 | }; 155 | /* End PBXProject section */ 156 | 157 | /* Begin PBXResourcesBuildPhase section */ 158 | 653F1008208685BC003D2635 /* Resources */ = { 159 | isa = PBXResourcesBuildPhase; 160 | buildActionMask = 2147483647; 161 | files = ( 162 | 653F1018208685BC003D2635 /* LaunchScreen.storyboard in Resources */, 163 | 653F1015208685BC003D2635 /* Assets.xcassets in Resources */, 164 | 653F1013208685BC003D2635 /* Main.storyboard in Resources */, 165 | ); 166 | runOnlyForDeploymentPostprocessing = 0; 167 | }; 168 | /* End PBXResourcesBuildPhase section */ 169 | 170 | /* Begin PBXSourcesBuildPhase section */ 171 | 653F1006208685BC003D2635 /* Sources */ = { 172 | isa = PBXSourcesBuildPhase; 173 | buildActionMask = 2147483647; 174 | files = ( 175 | 653F1010208685BC003D2635 /* ViewController.swift in Sources */, 176 | 653F102920868C31003D2635 /* RoundedView.swift in Sources */, 177 | 653F102820868C31003D2635 /* RoundedCollectionViewCell.swift in Sources */, 178 | 653F102720868C31003D2635 /* Roundable.swift in Sources */, 179 | 65453A0420A4279800248B57 /* CollectionViewCenterLayout.swift in Sources */, 180 | 653F100E208685BC003D2635 /* AppDelegate.swift in Sources */, 181 | ); 182 | runOnlyForDeploymentPostprocessing = 0; 183 | }; 184 | /* End PBXSourcesBuildPhase section */ 185 | 186 | /* Begin PBXVariantGroup section */ 187 | 653F1011208685BC003D2635 /* Main.storyboard */ = { 188 | isa = PBXVariantGroup; 189 | children = ( 190 | 653F1012208685BC003D2635 /* Base */, 191 | ); 192 | name = Main.storyboard; 193 | sourceTree = ""; 194 | }; 195 | 653F1016208685BC003D2635 /* LaunchScreen.storyboard */ = { 196 | isa = PBXVariantGroup; 197 | children = ( 198 | 653F1017208685BC003D2635 /* Base */, 199 | ); 200 | name = LaunchScreen.storyboard; 201 | sourceTree = ""; 202 | }; 203 | /* End PBXVariantGroup section */ 204 | 205 | /* Begin XCBuildConfiguration section */ 206 | 653F101A208685BC003D2635 /* Debug */ = { 207 | isa = XCBuildConfiguration; 208 | buildSettings = { 209 | ALWAYS_SEARCH_USER_PATHS = NO; 210 | CLANG_ANALYZER_NONNULL = YES; 211 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 212 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 213 | CLANG_CXX_LIBRARY = "libc++"; 214 | CLANG_ENABLE_MODULES = YES; 215 | CLANG_ENABLE_OBJC_ARC = YES; 216 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 217 | CLANG_WARN_BOOL_CONVERSION = YES; 218 | CLANG_WARN_COMMA = YES; 219 | CLANG_WARN_CONSTANT_CONVERSION = YES; 220 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 221 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 222 | CLANG_WARN_EMPTY_BODY = YES; 223 | CLANG_WARN_ENUM_CONVERSION = YES; 224 | CLANG_WARN_INFINITE_RECURSION = YES; 225 | CLANG_WARN_INT_CONVERSION = YES; 226 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 227 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 228 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 229 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 230 | CLANG_WARN_STRICT_PROTOTYPES = YES; 231 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 232 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 233 | CLANG_WARN_UNREACHABLE_CODE = YES; 234 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 235 | CODE_SIGN_IDENTITY = "iPhone Developer"; 236 | COPY_PHASE_STRIP = NO; 237 | DEBUG_INFORMATION_FORMAT = dwarf; 238 | ENABLE_STRICT_OBJC_MSGSEND = YES; 239 | ENABLE_TESTABILITY = YES; 240 | GCC_C_LANGUAGE_STANDARD = gnu11; 241 | GCC_DYNAMIC_NO_PIC = NO; 242 | GCC_NO_COMMON_BLOCKS = YES; 243 | GCC_OPTIMIZATION_LEVEL = 0; 244 | GCC_PREPROCESSOR_DEFINITIONS = ( 245 | "DEBUG=1", 246 | "$(inherited)", 247 | ); 248 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 249 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 250 | GCC_WARN_UNDECLARED_SELECTOR = YES; 251 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 252 | GCC_WARN_UNUSED_FUNCTION = YES; 253 | GCC_WARN_UNUSED_VARIABLE = YES; 254 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 255 | MTL_ENABLE_DEBUG_INFO = YES; 256 | ONLY_ACTIVE_ARCH = YES; 257 | SDKROOT = iphoneos; 258 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 259 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 260 | }; 261 | name = Debug; 262 | }; 263 | 653F101B208685BC003D2635 /* Release */ = { 264 | isa = XCBuildConfiguration; 265 | buildSettings = { 266 | ALWAYS_SEARCH_USER_PATHS = NO; 267 | CLANG_ANALYZER_NONNULL = YES; 268 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 269 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 270 | CLANG_CXX_LIBRARY = "libc++"; 271 | CLANG_ENABLE_MODULES = YES; 272 | CLANG_ENABLE_OBJC_ARC = YES; 273 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 274 | CLANG_WARN_BOOL_CONVERSION = YES; 275 | CLANG_WARN_COMMA = YES; 276 | CLANG_WARN_CONSTANT_CONVERSION = YES; 277 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 278 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 279 | CLANG_WARN_EMPTY_BODY = YES; 280 | CLANG_WARN_ENUM_CONVERSION = YES; 281 | CLANG_WARN_INFINITE_RECURSION = YES; 282 | CLANG_WARN_INT_CONVERSION = YES; 283 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 284 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 285 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 286 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 287 | CLANG_WARN_STRICT_PROTOTYPES = YES; 288 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 289 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 290 | CLANG_WARN_UNREACHABLE_CODE = YES; 291 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 292 | CODE_SIGN_IDENTITY = "iPhone Developer"; 293 | COPY_PHASE_STRIP = NO; 294 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 295 | ENABLE_NS_ASSERTIONS = NO; 296 | ENABLE_STRICT_OBJC_MSGSEND = YES; 297 | GCC_C_LANGUAGE_STANDARD = gnu11; 298 | GCC_NO_COMMON_BLOCKS = YES; 299 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 300 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 301 | GCC_WARN_UNDECLARED_SELECTOR = YES; 302 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 303 | GCC_WARN_UNUSED_FUNCTION = YES; 304 | GCC_WARN_UNUSED_VARIABLE = YES; 305 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 306 | MTL_ENABLE_DEBUG_INFO = NO; 307 | SDKROOT = iphoneos; 308 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 309 | VALIDATE_PRODUCT = YES; 310 | }; 311 | name = Release; 312 | }; 313 | 653F101D208685BC003D2635 /* Debug */ = { 314 | isa = XCBuildConfiguration; 315 | buildSettings = { 316 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 317 | CODE_SIGN_STYLE = Automatic; 318 | INFOPLIST_FILE = "$(SRCROOT)/CenteredCollectionView-Sample/Resources/Info.plist"; 319 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 320 | PRODUCT_BUNDLE_IDENTIFIER = "com.equaleyes.CenteredCollectionView-Sample"; 321 | PRODUCT_NAME = "$(TARGET_NAME)"; 322 | SWIFT_VERSION = 4.0; 323 | TARGETED_DEVICE_FAMILY = "1,2"; 324 | }; 325 | name = Debug; 326 | }; 327 | 653F101E208685BC003D2635 /* Release */ = { 328 | isa = XCBuildConfiguration; 329 | buildSettings = { 330 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 331 | CODE_SIGN_STYLE = Automatic; 332 | INFOPLIST_FILE = "$(SRCROOT)/CenteredCollectionView-Sample/Resources/Info.plist"; 333 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 334 | PRODUCT_BUNDLE_IDENTIFIER = "com.equaleyes.CenteredCollectionView-Sample"; 335 | PRODUCT_NAME = "$(TARGET_NAME)"; 336 | SWIFT_VERSION = 4.0; 337 | TARGETED_DEVICE_FAMILY = "1,2"; 338 | }; 339 | name = Release; 340 | }; 341 | /* End XCBuildConfiguration section */ 342 | 343 | /* Begin XCConfigurationList section */ 344 | 653F1005208685BC003D2635 /* Build configuration list for PBXProject "CenteredCollectionView-Sample" */ = { 345 | isa = XCConfigurationList; 346 | buildConfigurations = ( 347 | 653F101A208685BC003D2635 /* Debug */, 348 | 653F101B208685BC003D2635 /* Release */, 349 | ); 350 | defaultConfigurationIsVisible = 0; 351 | defaultConfigurationName = Release; 352 | }; 353 | 653F101C208685BC003D2635 /* Build configuration list for PBXNativeTarget "CenteredCollectionView-Sample" */ = { 354 | isa = XCConfigurationList; 355 | buildConfigurations = ( 356 | 653F101D208685BC003D2635 /* Debug */, 357 | 653F101E208685BC003D2635 /* Release */, 358 | ); 359 | defaultConfigurationIsVisible = 0; 360 | defaultConfigurationName = Release; 361 | }; 362 | /* End XCConfigurationList section */ 363 | }; 364 | rootObject = 653F1002208685BC003D2635 /* Project object */; 365 | } 366 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // CenteredCollectionView-Sample 4 | // 5 | // Created by Dejan Skledar on 17/04/2018. 6 | // Copyright © 2018 Dejan Skledar. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 17 | return true 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/Common/Roundable.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Roundable.swift 3 | // Autistica 4 | // 5 | // Created by Tomaž Ravljen on 04/04/2018. 6 | // Copyright © 2018 Equaleyes Solutions. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | protocol Roundable { 12 | var isCircle: Bool { get set } 13 | var cornerRadius: CGFloat { get set } 14 | var borderWidth: CGFloat { get set } 15 | var borderColor: UIColor { get set } 16 | } 17 | 18 | extension Roundable where Self: UIView { 19 | func setupLayer() { 20 | clipsToBounds = true 21 | 22 | if isCircle { 23 | layer.cornerRadius = frame.width < frame.height ? frame.width / 2 : frame.height / 2 24 | } else { 25 | layer.cornerRadius = cornerRadius 26 | } 27 | 28 | layer.borderWidth = borderWidth 29 | layer.borderColor = borderColor.cgColor 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/Common/RoundedCollectionViewCell.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RoundedCollectionViewCell.swift 3 | // Autistica 4 | // 5 | // Created by Dejan Skledar on 20/03/2018. 6 | // Copyright © 2018 Equaleyes Solutions. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class RoundedCollectionViewCell: UICollectionViewCell { 12 | @IBOutlet var textLabel: UILabel! 13 | } 14 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/Common/RoundedView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RoundedView.swift 3 | // iOS Equaleyes Foundation 4 | // 5 | // Created by Dejan Skledar on 23/10/2017. 6 | // Copyright © 2017 Equaleyes Solutions Ltd. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @IBDesignable 12 | class RoundedView: UIView, Roundable { 13 | @IBInspectable var isCircle: Bool = false { 14 | didSet { 15 | setupLayer() 16 | } 17 | } 18 | 19 | @IBInspectable var cornerRadius: CGFloat = 0 { 20 | didSet { 21 | setupLayer() 22 | } 23 | } 24 | 25 | @IBInspectable var borderWidth: CGFloat = 0 { 26 | didSet { 27 | setupLayer() 28 | } 29 | } 30 | 31 | @IBInspectable var borderColor: UIColor = .clear { 32 | didSet { 33 | setupLayer() 34 | } 35 | } 36 | 37 | override func layoutSubviews() { 38 | super.layoutSubviews() 39 | setupLayer() 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/Resources/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/Roundable.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Roundable.swift 3 | // Autistica 4 | // 5 | // Created by Tomaž Ravljen on 04/04/2018. 6 | // Copyright © 2018 Equaleyes Solutions. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | protocol Roundable { 12 | var isCircle: Bool { get set } 13 | var cornerRadius: CGFloat { get set } 14 | var borderWidth: CGFloat { get set } 15 | var borderColor: UIColor { get set } 16 | } 17 | 18 | extension Roundable where Self: UIView { 19 | func setupLayer() { 20 | clipsToBounds = true 21 | 22 | if isCircle { 23 | layer.cornerRadius = frame.width < frame.height ? frame.width / 2 : frame.height / 2 24 | } else { 25 | layer.cornerRadius = cornerRadius 26 | } 27 | 28 | layer.borderWidth = borderWidth 29 | layer.borderColor = borderColor.cgColor 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/RoundedCollectionViewCell.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RoundedCollectionViewCell.swift 3 | // Autistica 4 | // 5 | // Created by Dejan Skledar on 20/03/2018. 6 | // Copyright © 2018 Equaleyes Solutions. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class RoundedCollectionViewCell: UICollectionViewCell { 12 | @IBOutlet var textLabel: UILabel! 13 | @IBOutlet var roundedView: RoundedView! 14 | 15 | override var isSelected: Bool { 16 | didSet { 17 | setNeedsLayout() 18 | layoutIfNeeded() 19 | } 20 | } 21 | 22 | override func layoutSubviews() { 23 | super.layoutSubviews() 24 | roundedView.borderWidth = isSelected ? 0 : 1 25 | roundedView.backgroundColor = isSelected ? .darkSkyBlue : .clear 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/Storyboards/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/Storyboards/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /CenteredCollectionView-Sample/CenteredCollectionView-Sample/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // CenteredCollectionView-Sample 4 | // 5 | // Created by Dejan Skledar on 17/04/2018. 6 | // Copyright © 2018 Dejan Skledar. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 12 | @IBOutlet var collectionView: UICollectionView! 13 | 14 | private let titles = [ 15 | "cersei", 16 | "daenerys the stormborn", 17 | "lannister", 18 | "snow the bastard", 19 | "stark", 20 | "baratheon", 21 | "tyrion the dworf" 22 | ] 23 | 24 | override func viewDidLoad() { 25 | super.viewDidLoad() 26 | let layout = UICollectionViewCenterLayout() 27 | layout.estimatedItemSize = CGSize(width: 140, height: 40) 28 | collectionView.collectionViewLayout = layout 29 | } 30 | 31 | // MARK - UICollectionViewDataSource 32 | 33 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 34 | return titles.count 35 | } 36 | 37 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 38 | guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "titleCell", 39 | for: indexPath) as? RoundedCollectionViewCell else { 40 | return RoundedCollectionViewCell() 41 | } 42 | cell.textLabel.text = titles[indexPath.row] 43 | return cell 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /CollectionViewCenterLayout.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CollectionViewCenterLayout.swift 3 | // CenteredCollectionView-Sample 4 | // 5 | // Created by Dejan Skledar on 17/04/2018. 6 | // Copyright © 2018 Dejan Skledar. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class CollectionViewRow { 12 | var attributes = [UICollectionViewLayoutAttributes]() 13 | var spacing: CGFloat = 0 14 | 15 | 16 | 17 | init(spacing: CGFloat) { 18 | self.spacing = spacing 19 | } 20 | 21 | func add(attribute: UICollectionViewLayoutAttributes) { 22 | attributes.append(attribute) 23 | } 24 | 25 | var rowWidth: CGFloat { 26 | return attributes.reduce(0, { result, attribute -> CGFloat in 27 | return result + attribute.frame.width 28 | }) + CGFloat(attributes.count - 1) * spacing 29 | } 30 | 31 | func centerLayout(collectionViewWidth: CGFloat) { 32 | let padding = (collectionViewWidth - rowWidth) / 2 33 | var offset = padding 34 | for attribute in attributes { 35 | attribute.frame.origin.x = offset 36 | offset += attribute.frame.width + spacing 37 | } 38 | } 39 | } 40 | 41 | class UICollectionViewCenterLayout: UICollectionViewFlowLayout { 42 | override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 43 | guard let attributes = super.layoutAttributesForElements(in: rect) else { 44 | return nil 45 | } 46 | 47 | var rows = [CollectionViewRow]() 48 | var currentRowY: CGFloat = -1 49 | 50 | for attribute in attributes { 51 | if currentRowY != attribute.frame.midY { 52 | currentRowY = attribute.frame.midY 53 | rows.append(CollectionViewRow(spacing: 10)) 54 | } 55 | rows.last?.add(attribute: attribute) 56 | } 57 | 58 | rows.forEach { $0.centerLayout(collectionViewWidth: collectionView?.frame.width ?? 0) } 59 | return rows.flatMap { $0.attributes } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Dejan Skledar 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sample of a Centered CollectionView Flow Layout 2 | 3 | This is a sample of a Centered CollectionView Flow Layout, that is in detail explaing in my blog post here. 4 | 5 | The result of the layout looks like: 6 | 7 | CenteredCollectionViewFlowLayout 8 | 9 | ## Usage 10 | Just initialize the `UICollectionViewCenterLayout` object and set it as the `collectionViewLayout` of your `UICollectionView`: 11 | 12 | ```swift 13 | let layout = UICollectionViewCenterLayout() 14 | layout.estimatedItemSize = CGSize(width: 140, height: 40) 15 | collectionView.collectionViewLayout = layout 16 | ``` 17 | --------------------------------------------------------------------------------