NCLayout.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // NCLayout.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/11/2018.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. class NCListLayout: UICollectionViewFlowLayout {
  25. let itemHeight: CGFloat = 60
  26. override init() {
  27. super.init()
  28. sectionHeadersPinToVisibleBounds = false
  29. minimumInteritemSpacing = 0
  30. minimumLineSpacing = 1
  31. self.scrollDirection = .vertical
  32. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  33. }
  34. required init?(coder aDecoder: NSCoder) {
  35. fatalError("init(coder:) has not been implemented")
  36. }
  37. override var itemSize: CGSize {
  38. get {
  39. if let collectionView = collectionView {
  40. let itemWidth: CGFloat = collectionView.frame.width
  41. return CGSize(width: itemWidth, height: self.itemHeight)
  42. }
  43. // Default fallback
  44. return CGSize(width: 100, height: 100)
  45. }
  46. set {
  47. super.itemSize = newValue
  48. }
  49. }
  50. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  51. return proposedContentOffset
  52. }
  53. }
  54. class NCGridLayout: UICollectionViewFlowLayout {
  55. let heightLabelPlusButton: CGFloat = 45
  56. let preferenceWidth: CGFloat = 110
  57. let marginLeftRight: CGFloat = 1
  58. override init() {
  59. super.init()
  60. sectionHeadersPinToVisibleBounds = false
  61. minimumInteritemSpacing = 1
  62. minimumLineSpacing = 1
  63. self.scrollDirection = .vertical
  64. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  65. }
  66. required init?(coder aDecoder: NSCoder) {
  67. fatalError("init(coder:) has not been implemented")
  68. }
  69. override var itemSize: CGSize {
  70. get {
  71. if let collectionView = collectionView {
  72. let numItems: Int = Int(collectionView.frame.width / preferenceWidth)
  73. let itemWidth: CGFloat = (collectionView.frame.width - (marginLeftRight * 2) - CGFloat(numItems)) / CGFloat(numItems)
  74. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  75. return CGSize(width: itemWidth, height: itemHeight)
  76. }
  77. // Default fallback
  78. return CGSize(width: 100, height: 100)
  79. }
  80. set {
  81. super.itemSize = newValue
  82. }
  83. }
  84. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  85. return proposedContentOffset
  86. }
  87. }