├── .gitignore ├── FloatingHeadersDemo ├── ASFloatingHeadersFlowLayout │ └── ASFloatingHeadersFlowLayout.swift ├── FloatingHeadersDemo.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ └── contents.xcworkspacedata │ └── xcshareddata │ │ └── xcschemes │ │ └── FloatingHeadersDemo.xcscheme └── FloatingHeadersDemo │ ├── AppDelegate.swift │ ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard │ ├── CollectionReusableView.swift │ ├── DemoGridViewController.swift │ ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ └── Info.plist ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/ASFloatingHeadersFlowLayout/ASFloatingHeadersFlowLayout.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ASFloatingHeadersFlowLayout.swift 3 | // FloatingHeadersDemo 4 | // 5 | // Created by Andrey Syvrachev on 22.04.15. 6 | // Copyright (c) 2015 Andrey Syvrachev. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ASFloatingHeadersFlowLayout: UICollectionViewFlowLayout { 12 | 13 | var sectionAttributes:[(header:UICollectionViewLayoutAttributes!,sectionEnd:CGFloat!)] = [] 14 | let offsets = NSMutableOrderedSet() 15 | var floatingSectionIndex:Int! = nil 16 | var width:CGFloat! = nil 17 | 18 | override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { 19 | return true 20 | } 21 | 22 | override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 23 | 24 | let attrs = super.layoutAttributesForElementsInRect(rect) 25 | let ret = attrs?.map() { 26 | 27 | (attribute) -> UICollectionViewLayoutAttributes in 28 | 29 | let attr = attribute 30 | 31 | if let elementKind = attr.representedElementKind { 32 | if (elementKind == UICollectionElementKindSectionHeader){ 33 | return self.sectionAttributes[attr.indexPath.section].header 34 | } 35 | } 36 | 37 | return attr 38 | } 39 | return ret 40 | } 41 | 42 | override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { 43 | 44 | let collectionView = self.collectionView! 45 | 46 | let offset:CGFloat = collectionView.contentOffset.y + collectionView.contentInset.top 47 | let index = indexForOffset(offset) 48 | 49 | let section = self.sectionAttributes[index] 50 | 51 | let maxOffsetForHeader = section.sectionEnd - section.header.frame.size.height 52 | let headerResultOffset = offset > 0 ? min(offset,maxOffsetForHeader) : 0 53 | 54 | let headerAttrs = section.header 55 | headerAttrs.frame = CGRectMake(0, headerResultOffset, headerAttrs.frame.size.width, headerAttrs.frame.size.height) 56 | headerAttrs.zIndex = 1024 57 | 58 | let attrs = self.sectionAttributes[indexPath.section] 59 | return elementKind == UICollectionElementKindSectionHeader ? attrs.header : self.layoutAttributesForSupplementaryViewOfKind(elementKind, atIndexPath: indexPath) 60 | } 61 | 62 | private func testWidthChanged(newWidth:CGFloat!) -> Bool { 63 | if let width = self.width{ 64 | if (width != newWidth){ 65 | self.width = newWidth 66 | return true 67 | } 68 | } 69 | self.width = newWidth 70 | return false 71 | } 72 | 73 | override func invalidationContextForBoundsChange(newBounds: CGRect) -> UICollectionViewLayoutInvalidationContext { 74 | 75 | let context = super.invalidationContextForBoundsChange(newBounds) 76 | 77 | if (self.testWidthChanged(newBounds.size.width)){ 78 | return context 79 | } 80 | 81 | let collectionView = self.collectionView! 82 | 83 | let offset:CGFloat = newBounds.origin.y + collectionView.contentInset.top 84 | let index = indexForOffset(offset) 85 | 86 | var invalidatedIndexPaths = [NSIndexPath(forItem: 0, inSection:index)]; 87 | if let floatingSectionIndex = self.floatingSectionIndex { 88 | if (self.floatingSectionIndex != index){ 89 | 90 | // have to restore previous section attributes to default 91 | self.sectionAttributes[floatingSectionIndex].header = super.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader,atIndexPath: NSIndexPath(forItem: 0, inSection: floatingSectionIndex)) 92 | 93 | invalidatedIndexPaths.append(NSIndexPath(forItem: 0, inSection:floatingSectionIndex)) 94 | } 95 | } 96 | self.floatingSectionIndex = index 97 | 98 | context.invalidateSupplementaryElementsOfKind(UICollectionElementKindSectionHeader,atIndexPaths:invalidatedIndexPaths) 99 | return context 100 | } 101 | 102 | override func prepareLayout() { 103 | 104 | super.prepareLayout() 105 | 106 | self.sectionAttributes.removeAll(keepCapacity: true) 107 | self.offsets.removeAllObjects() 108 | 109 | let collectionView = self.collectionView! 110 | 111 | let numberOfSections = collectionView.numberOfSections() 112 | for var section = 0; section < numberOfSections; ++section { 113 | 114 | let indexPath = NSIndexPath(forItem: 0, inSection: section) 115 | let header = super.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader,atIndexPath:indexPath) 116 | 117 | var sectionEnd = header!.frame.origin.y + header!.frame.size.height 118 | let numberOfItemsInSection = collectionView.numberOfItemsInSection(section) 119 | if (numberOfItemsInSection > 0){ 120 | let lastItemAttrs = super.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: numberOfItemsInSection - 1, inSection: section)) 121 | sectionEnd = lastItemAttrs!.frame.origin.y + lastItemAttrs!.frame.size.height + self.sectionInset.bottom 122 | } 123 | let sectionInfo:(header:UICollectionViewLayoutAttributes!,sectionEnd:CGFloat!) = (header:header,sectionEnd:sectionEnd) 124 | self.sectionAttributes.append(sectionInfo) 125 | 126 | if (section > 0){ 127 | self.offsets.addObject(header!.frame.origin.y) 128 | } 129 | } 130 | } 131 | 132 | 133 | private func indexForOffset(offset: CGFloat) -> Int { 134 | 135 | let range = NSRange(location:0, length:self.offsets.count) 136 | return self.offsets.indexOfObject(offset, 137 | inSortedRange: range, 138 | options: .InsertionIndex, 139 | usingComparator: { (section0:AnyObject!, section1:AnyObject!) -> NSComparisonResult in 140 | let s0:CGFloat = section0 as! CGFloat 141 | let s1:CGFloat = section1 as! CGFloat 142 | return s0 < s1 ? .OrderedAscending : .OrderedDescending 143 | }) 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1086B7A91AE139FC00D3B923 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1086B7A81AE139FC00D3B923 /* AppDelegate.swift */; }; 11 | 1086B7AB1AE139FC00D3B923 /* DemoGridViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1086B7AA1AE139FC00D3B923 /* DemoGridViewController.swift */; }; 12 | 1086B7AE1AE139FC00D3B923 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1086B7AC1AE139FC00D3B923 /* Main.storyboard */; }; 13 | 1086B7B01AE139FC00D3B923 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1086B7AF1AE139FC00D3B923 /* Images.xcassets */; }; 14 | 1086B7B31AE139FC00D3B923 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1086B7B11AE139FC00D3B923 /* LaunchScreen.xib */; }; 15 | 10AEE9D21AE7D6C800878BEE /* ASFloatingHeadersFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 10AEE9D11AE7D6C800878BEE /* ASFloatingHeadersFlowLayout.swift */; }; 16 | 10AEE9D41AE7D7FA00878BEE /* CollectionReusableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 10AEE9D31AE7D7FA00878BEE /* CollectionReusableView.swift */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 1086B7A31AE139FC00D3B923 /* FloatingHeadersDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FloatingHeadersDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 1086B7A71AE139FC00D3B923 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 22 | 1086B7A81AE139FC00D3B923 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 23 | 1086B7AA1AE139FC00D3B923 /* DemoGridViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoGridViewController.swift; sourceTree = ""; }; 24 | 1086B7AD1AE139FC00D3B923 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 25 | 1086B7AF1AE139FC00D3B923 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 26 | 1086B7B21AE139FC00D3B923 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 27 | 10AEE9D11AE7D6C800878BEE /* ASFloatingHeadersFlowLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ASFloatingHeadersFlowLayout.swift; path = ASFloatingHeadersFlowLayout/ASFloatingHeadersFlowLayout.swift; sourceTree = SOURCE_ROOT; }; 28 | 10AEE9D31AE7D7FA00878BEE /* CollectionReusableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionReusableView.swift; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | 1086B7A01AE139FC00D3B923 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | 1086B79A1AE139FC00D3B923 = { 43 | isa = PBXGroup; 44 | children = ( 45 | 10AEE9D01AE7D62600878BEE /* ASFloatingHeadersLayout */, 46 | 1086B7A51AE139FC00D3B923 /* FloatingHeadersDemo */, 47 | 1086B7A41AE139FC00D3B923 /* Products */, 48 | ); 49 | sourceTree = ""; 50 | }; 51 | 1086B7A41AE139FC00D3B923 /* Products */ = { 52 | isa = PBXGroup; 53 | children = ( 54 | 1086B7A31AE139FC00D3B923 /* FloatingHeadersDemo.app */, 55 | ); 56 | name = Products; 57 | sourceTree = ""; 58 | }; 59 | 1086B7A51AE139FC00D3B923 /* FloatingHeadersDemo */ = { 60 | isa = PBXGroup; 61 | children = ( 62 | 1086B7A81AE139FC00D3B923 /* AppDelegate.swift */, 63 | 1086B7AA1AE139FC00D3B923 /* DemoGridViewController.swift */, 64 | 1086B7AC1AE139FC00D3B923 /* Main.storyboard */, 65 | 1086B7AF1AE139FC00D3B923 /* Images.xcassets */, 66 | 1086B7B11AE139FC00D3B923 /* LaunchScreen.xib */, 67 | 1086B7A61AE139FC00D3B923 /* Supporting Files */, 68 | 10AEE9D31AE7D7FA00878BEE /* CollectionReusableView.swift */, 69 | ); 70 | path = FloatingHeadersDemo; 71 | sourceTree = ""; 72 | }; 73 | 1086B7A61AE139FC00D3B923 /* Supporting Files */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 1086B7A71AE139FC00D3B923 /* Info.plist */, 77 | ); 78 | name = "Supporting Files"; 79 | sourceTree = ""; 80 | }; 81 | 10AEE9D01AE7D62600878BEE /* ASFloatingHeadersLayout */ = { 82 | isa = PBXGroup; 83 | children = ( 84 | 10AEE9D11AE7D6C800878BEE /* ASFloatingHeadersFlowLayout.swift */, 85 | ); 86 | name = ASFloatingHeadersLayout; 87 | path = FloatingHeadersDemo; 88 | sourceTree = ""; 89 | }; 90 | /* End PBXGroup section */ 91 | 92 | /* Begin PBXNativeTarget section */ 93 | 1086B7A21AE139FC00D3B923 /* FloatingHeadersDemo */ = { 94 | isa = PBXNativeTarget; 95 | buildConfigurationList = 1086B7C21AE139FC00D3B923 /* Build configuration list for PBXNativeTarget "FloatingHeadersDemo" */; 96 | buildPhases = ( 97 | 1086B79F1AE139FC00D3B923 /* Sources */, 98 | 1086B7A01AE139FC00D3B923 /* Frameworks */, 99 | 1086B7A11AE139FC00D3B923 /* Resources */, 100 | ); 101 | buildRules = ( 102 | ); 103 | dependencies = ( 104 | ); 105 | name = FloatingHeadersDemo; 106 | productName = FloatingHeadersDemo; 107 | productReference = 1086B7A31AE139FC00D3B923 /* FloatingHeadersDemo.app */; 108 | productType = "com.apple.product-type.application"; 109 | }; 110 | /* End PBXNativeTarget section */ 111 | 112 | /* Begin PBXProject section */ 113 | 1086B79B1AE139FC00D3B923 /* Project object */ = { 114 | isa = PBXProject; 115 | attributes = { 116 | LastSwiftMigration = 0700; 117 | LastSwiftUpdateCheck = 0700; 118 | LastUpgradeCheck = 0700; 119 | ORGANIZATIONNAME = "Andrey Syvrachev"; 120 | TargetAttributes = { 121 | 1086B7A21AE139FC00D3B923 = { 122 | CreatedOnToolsVersion = 6.3; 123 | }; 124 | }; 125 | }; 126 | buildConfigurationList = 1086B79E1AE139FC00D3B923 /* Build configuration list for PBXProject "FloatingHeadersDemo" */; 127 | compatibilityVersion = "Xcode 3.2"; 128 | developmentRegion = English; 129 | hasScannedForEncodings = 0; 130 | knownRegions = ( 131 | en, 132 | Base, 133 | ); 134 | mainGroup = 1086B79A1AE139FC00D3B923; 135 | productRefGroup = 1086B7A41AE139FC00D3B923 /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | 1086B7A21AE139FC00D3B923 /* FloatingHeadersDemo */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXResourcesBuildPhase section */ 145 | 1086B7A11AE139FC00D3B923 /* Resources */ = { 146 | isa = PBXResourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | 1086B7AE1AE139FC00D3B923 /* Main.storyboard in Resources */, 150 | 1086B7B31AE139FC00D3B923 /* LaunchScreen.xib in Resources */, 151 | 1086B7B01AE139FC00D3B923 /* Images.xcassets in Resources */, 152 | ); 153 | runOnlyForDeploymentPostprocessing = 0; 154 | }; 155 | /* End PBXResourcesBuildPhase section */ 156 | 157 | /* Begin PBXSourcesBuildPhase section */ 158 | 1086B79F1AE139FC00D3B923 /* Sources */ = { 159 | isa = PBXSourcesBuildPhase; 160 | buildActionMask = 2147483647; 161 | files = ( 162 | 10AEE9D41AE7D7FA00878BEE /* CollectionReusableView.swift in Sources */, 163 | 10AEE9D21AE7D6C800878BEE /* ASFloatingHeadersFlowLayout.swift in Sources */, 164 | 1086B7AB1AE139FC00D3B923 /* DemoGridViewController.swift in Sources */, 165 | 1086B7A91AE139FC00D3B923 /* AppDelegate.swift in Sources */, 166 | ); 167 | runOnlyForDeploymentPostprocessing = 0; 168 | }; 169 | /* End PBXSourcesBuildPhase section */ 170 | 171 | /* Begin PBXVariantGroup section */ 172 | 1086B7AC1AE139FC00D3B923 /* Main.storyboard */ = { 173 | isa = PBXVariantGroup; 174 | children = ( 175 | 1086B7AD1AE139FC00D3B923 /* Base */, 176 | ); 177 | name = Main.storyboard; 178 | sourceTree = ""; 179 | }; 180 | 1086B7B11AE139FC00D3B923 /* LaunchScreen.xib */ = { 181 | isa = PBXVariantGroup; 182 | children = ( 183 | 1086B7B21AE139FC00D3B923 /* Base */, 184 | ); 185 | name = LaunchScreen.xib; 186 | sourceTree = ""; 187 | }; 188 | /* End PBXVariantGroup section */ 189 | 190 | /* Begin XCBuildConfiguration section */ 191 | 1086B7C01AE139FC00D3B923 /* Debug */ = { 192 | isa = XCBuildConfiguration; 193 | buildSettings = { 194 | ALWAYS_SEARCH_USER_PATHS = NO; 195 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 196 | CLANG_CXX_LIBRARY = "libc++"; 197 | CLANG_ENABLE_MODULES = YES; 198 | CLANG_ENABLE_OBJC_ARC = YES; 199 | CLANG_WARN_BOOL_CONVERSION = YES; 200 | CLANG_WARN_CONSTANT_CONVERSION = YES; 201 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 202 | CLANG_WARN_EMPTY_BODY = YES; 203 | CLANG_WARN_ENUM_CONVERSION = YES; 204 | CLANG_WARN_INT_CONVERSION = YES; 205 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 206 | CLANG_WARN_UNREACHABLE_CODE = YES; 207 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 208 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 209 | COPY_PHASE_STRIP = NO; 210 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 211 | ENABLE_STRICT_OBJC_MSGSEND = YES; 212 | ENABLE_TESTABILITY = YES; 213 | GCC_C_LANGUAGE_STANDARD = gnu99; 214 | GCC_DYNAMIC_NO_PIC = NO; 215 | GCC_NO_COMMON_BLOCKS = YES; 216 | GCC_OPTIMIZATION_LEVEL = 0; 217 | GCC_PREPROCESSOR_DEFINITIONS = ( 218 | "DEBUG=1", 219 | "$(inherited)", 220 | ); 221 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 222 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 223 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 224 | GCC_WARN_UNDECLARED_SELECTOR = YES; 225 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 226 | GCC_WARN_UNUSED_FUNCTION = YES; 227 | GCC_WARN_UNUSED_VARIABLE = YES; 228 | IPHONEOS_DEPLOYMENT_TARGET = 8.3; 229 | LD_RUNPATH_SEARCH_PATHS = ""; 230 | MTL_ENABLE_DEBUG_INFO = YES; 231 | ONLY_ACTIVE_ARCH = YES; 232 | SDKROOT = iphoneos; 233 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 234 | TARGETED_DEVICE_FAMILY = "1,2"; 235 | }; 236 | name = Debug; 237 | }; 238 | 1086B7C11AE139FC00D3B923 /* Release */ = { 239 | isa = XCBuildConfiguration; 240 | buildSettings = { 241 | ALWAYS_SEARCH_USER_PATHS = NO; 242 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 243 | CLANG_CXX_LIBRARY = "libc++"; 244 | CLANG_ENABLE_MODULES = YES; 245 | CLANG_ENABLE_OBJC_ARC = YES; 246 | CLANG_WARN_BOOL_CONVERSION = YES; 247 | CLANG_WARN_CONSTANT_CONVERSION = YES; 248 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 249 | CLANG_WARN_EMPTY_BODY = YES; 250 | CLANG_WARN_ENUM_CONVERSION = YES; 251 | CLANG_WARN_INT_CONVERSION = YES; 252 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 253 | CLANG_WARN_UNREACHABLE_CODE = YES; 254 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 255 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 256 | COPY_PHASE_STRIP = NO; 257 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 258 | ENABLE_NS_ASSERTIONS = NO; 259 | ENABLE_STRICT_OBJC_MSGSEND = YES; 260 | GCC_C_LANGUAGE_STANDARD = gnu99; 261 | GCC_NO_COMMON_BLOCKS = YES; 262 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 263 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 264 | GCC_WARN_UNDECLARED_SELECTOR = YES; 265 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 266 | GCC_WARN_UNUSED_FUNCTION = YES; 267 | GCC_WARN_UNUSED_VARIABLE = YES; 268 | IPHONEOS_DEPLOYMENT_TARGET = 8.3; 269 | LD_RUNPATH_SEARCH_PATHS = ""; 270 | MTL_ENABLE_DEBUG_INFO = NO; 271 | SDKROOT = iphoneos; 272 | TARGETED_DEVICE_FAMILY = "1,2"; 273 | VALIDATE_PRODUCT = YES; 274 | }; 275 | name = Release; 276 | }; 277 | 1086B7C31AE139FC00D3B923 /* Debug */ = { 278 | isa = XCBuildConfiguration; 279 | buildSettings = { 280 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 281 | INFOPLIST_FILE = FloatingHeadersDemo/Info.plist; 282 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 283 | LIBRARY_SEARCH_PATHS = ""; 284 | PRODUCT_BUNDLE_IDENTIFIER = "allright.$(PRODUCT_NAME:rfc1034identifier)"; 285 | PRODUCT_NAME = "$(TARGET_NAME)"; 286 | }; 287 | name = Debug; 288 | }; 289 | 1086B7C41AE139FC00D3B923 /* Release */ = { 290 | isa = XCBuildConfiguration; 291 | buildSettings = { 292 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 293 | INFOPLIST_FILE = FloatingHeadersDemo/Info.plist; 294 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 295 | LIBRARY_SEARCH_PATHS = ""; 296 | PRODUCT_BUNDLE_IDENTIFIER = "allright.$(PRODUCT_NAME:rfc1034identifier)"; 297 | PRODUCT_NAME = "$(TARGET_NAME)"; 298 | }; 299 | name = Release; 300 | }; 301 | /* End XCBuildConfiguration section */ 302 | 303 | /* Begin XCConfigurationList section */ 304 | 1086B79E1AE139FC00D3B923 /* Build configuration list for PBXProject "FloatingHeadersDemo" */ = { 305 | isa = XCConfigurationList; 306 | buildConfigurations = ( 307 | 1086B7C01AE139FC00D3B923 /* Debug */, 308 | 1086B7C11AE139FC00D3B923 /* Release */, 309 | ); 310 | defaultConfigurationIsVisible = 0; 311 | defaultConfigurationName = Release; 312 | }; 313 | 1086B7C21AE139FC00D3B923 /* Build configuration list for PBXNativeTarget "FloatingHeadersDemo" */ = { 314 | isa = XCConfigurationList; 315 | buildConfigurations = ( 316 | 1086B7C31AE139FC00D3B923 /* Debug */, 317 | 1086B7C41AE139FC00D3B923 /* Release */, 318 | ); 319 | defaultConfigurationIsVisible = 0; 320 | defaultConfigurationName = Release; 321 | }; 322 | /* End XCConfigurationList section */ 323 | }; 324 | rootObject = 1086B79B1AE139FC00D3B923 /* Project object */; 325 | } 326 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo.xcodeproj/xcshareddata/xcschemes/FloatingHeadersDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 29 | 35 | 36 | 37 | 38 | 39 | 44 | 45 | 47 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | 66 | 67 | 68 | 78 | 80 | 86 | 87 | 88 | 89 | 90 | 91 | 97 | 99 | 105 | 106 | 107 | 108 | 110 | 111 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // FloatingHeadersDemo 4 | // 5 | // Created by Andrey Syvrachev on 17.04.15. 6 | // Copyright (c) 2015 Andrey Syvrachev. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo/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 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 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 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo/CollectionReusableView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SectionHeader.swift 3 | // FloatingHeadersDemo 4 | // 5 | // Created by Andrey Syvrachev on 22.04.15. 6 | // Copyright (c) 2015 Andrey Syvrachev. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | 12 | class CollectionReusableView: UICollectionReusableView { 13 | 14 | @IBOutlet var label: UILabel! 15 | 16 | // let labelLeft = UILabel() 17 | // let labelRight = UILabel() 18 | // 19 | // var section: Int? { 20 | // get { 21 | // return self.labelLeft.text?.toInt() 22 | // } 23 | // set(section){ 24 | // self.labelLeft.text = "Section: \(section!)" 25 | // } 26 | // } 27 | // 28 | // var right: String? { 29 | // get { 30 | // return self.labelRight.text 31 | // } 32 | // set(text){ 33 | // self.labelRight.text = text 34 | // } 35 | // } 36 | // 37 | // override init(frame: CGRect) { 38 | // super.init(frame: frame) 39 | // 40 | // self.backgroundColor = UIColor(white: 0.15, alpha:0.5) 41 | // 42 | // self.addSubview(labelLeft) 43 | // self.addSubview(labelRight) 44 | // 45 | // self.labelLeft.textColor = .whiteColor() 46 | // self.labelLeft.text = "20 марта" 47 | // 48 | // self.labelRight.textColor = .whiteColor() 49 | // self.labelRight.text = "19:25" 50 | // self.labelRight.textAlignment = .Right 51 | // } 52 | // 53 | // override func layoutSubviews() { 54 | // super.layoutSubviews() 55 | // self.labelLeft.frame = self.bounds 56 | // self.labelRight.frame = self.bounds 57 | // 58 | // } 59 | // 60 | // required init(coder aDecoder: NSCoder) { 61 | // fatalError("init(coder:) has not been implemented") 62 | // } 63 | } 64 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo/DemoGridViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // FloatingHeadersDemo 4 | // 5 | // Created by Andrey Syvrachev on 17.04.15. 6 | // Copyright (c) 2015 Andrey Syvrachev. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | let CellReuseId = "Cell" 12 | let HeaderReuseId = "Header" 13 | let FooterReuseId = "Footer" 14 | 15 | class DemoGridViewController: UICollectionViewController { 16 | 17 | override func viewDidLoad() { 18 | super.viewDidLoad() 19 | // Do any additional setup after loading the view, typically from a nib. 20 | } 21 | 22 | override func didReceiveMemoryWarning() { 23 | super.didReceiveMemoryWarning() 24 | // Dispose of any resources that can be recreated. 25 | } 26 | 27 | 28 | override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 29 | return 1000 30 | } 31 | 32 | override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 33 | return section%20 + 1 34 | } 35 | 36 | override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 37 | return collectionView.dequeueReusableCellWithReuseIdentifier(CellReuseId, forIndexPath: indexPath) 38 | } 39 | 40 | override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 41 | 42 | let reuseId = kind == UICollectionElementKindSectionHeader ? HeaderReuseId : FooterReuseId 43 | let collectionReusableView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: reuseId, forIndexPath: indexPath) as! CollectionReusableView 44 | 45 | collectionReusableView.label.text = "\(reuseId): \(indexPath.section)" 46 | 47 | return collectionReusableView 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /FloatingHeadersDemo/FloatingHeadersDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 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 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 allright 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ASFloatingHeadersFlowLayout 2 | 3 | High performance implementation of CollectionView Flow Layout for Sticky/Floating headers behavior like in table view. 4 | Used new iOS7/8 features with partial invalidation. 5 | 6 | #Update for iOS9 7 | There is no more need to use ASFloatingHeadersFlowLayout in iOS9. Apple has been implement this functional, you can set to true 8 | sectionHeadersPinToVisibleBounds/sectionFootersPinToVisibleBounds properties in UICollectionViewFlowLayout. 9 | 10 | ```swift 11 | 12 | @available(iOS 6.0, *) 13 | public class UICollectionViewFlowLayout : UICollectionViewLayout { 14 | 15 | public var minimumLineSpacing: CGFloat 16 | public var minimumInteritemSpacing: CGFloat 17 | public var itemSize: CGSize 18 | @available(iOS 8.0, *) 19 | public var estimatedItemSize: CGSize // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -preferredLayoutAttributesFittingAttributes: 20 | public var scrollDirection: UICollectionViewScrollDirection // default is UICollectionViewScrollDirectionVertical 21 | public var headerReferenceSize: CGSize 22 | public var footerReferenceSize: CGSize 23 | public var sectionInset: UIEdgeInsets 24 | 25 | // Set these properties to YES to get headers that pin to the top of the screen and footers that pin to the bottom while scrolling (similar to UITableView). 26 | @available(iOS 9.0, *) 27 | public var sectionHeadersPinToVisibleBounds: Bool 28 | @available(iOS 9.0, *) 29 | public var sectionFootersPinToVisibleBounds: Bool 30 | } 31 | --------------------------------------------------------------------------------