-
Notifications
You must be signed in to change notification settings - Fork 90
/
ProfileCollectionViewFlowLayout.swift
94 lines (77 loc) · 3.87 KB
/
ProfileCollectionViewFlowLayout.swift
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
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
//
// ProfileViewFlowLayout.swift
// KD Tiktok-Clone
//
// Created by Sam Ding on 9/11/20.
// Copyright © 2020 Kaishan. All rights reserved.
//
import Foundation
import UIKit
class ProfileCollectionViewFlowLayout: UICollectionViewFlowLayout {
/// Height for Navigation Bar
var navBarHeight: CGFloat = 0
init(navBarHeight: CGFloat){
super.init()
self.navBarHeight = navBarHeight
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var attributesArray: [UICollectionViewLayoutAttributes] = super.layoutAttributesForElements(in: rect)!
let copyArray = attributesArray
for index in 0..<copyArray.count {
let attributes = copyArray[index]
//Remove all headers and footers
if attributes.representedElementKind == UICollectionView.elementKindSectionHeader || attributes.representedElementKind == UICollectionView.elementKindSectionFooter {
if let idx = attributesArray.firstIndex(of: attributes) {
attributesArray.remove(at: idx)
}
}
}
//Append Two headers into the first and the second section. 1 -> Profile Display 2 -> Slide Bar
if let header = super.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: IndexPath.init(item: 0, section: 0)) {
attributesArray.append(header)
}
if let slideBar = super.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: IndexPath.init(item: 0, section: 1)) {
attributesArray.append(slideBar)
}
for attributes in attributesArray {
if attributes.indexPath.section == 0 {
if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
var rect = attributes.frame
if (self.collectionView?.contentOffset.y)! + self.navBarHeight - rect.size.height > rect.origin.y {
rect.origin.y = (self.collectionView?.contentOffset.y)! + self.navBarHeight - rect.size.height
attributes.frame = rect
}
// Stretchy Header
// let contentOffsetY = collectionView!.contentOffset.y /// Negative Value
// if contentOffsetY < 0 {
// let width = collectionView!.frame.width
// // as contentOffsetY is -ve, the height will increase based on contentOffsetY
// let height = attributes.frame.height - contentOffsetY
// attributes.frame = CGRect(x: 0, y: contentOffsetY, width: width, height: height)
// }
// Set z position of SlideBar to the second highest
attributes.zIndex = 5
}
}
if attributes.indexPath.section == 1{
if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
var rect = attributes.frame
if (self.collectionView?.contentOffset.y)! + self.navBarHeight > rect.origin.y {
rect.origin.y = (self.collectionView?.contentOffset.y)! + self.navBarHeight
attributes.frame = rect
}
// Set z position of SlideBar to the highest
attributes.zIndex = 10
}
}
}
return attributesArray
}
// Allow Invalidating Layout
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}