NCLayout.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. var heightLabelPlusButton: CGFloat = 45
  56. var preferenceWidth: CGFloat = 110
  57. var marginLeftRight: CGFloat = 1
  58. var numItems: Int = 0
  59. override init() {
  60. super.init()
  61. sectionHeadersPinToVisibleBounds = false
  62. minimumInteritemSpacing = 1
  63. minimumLineSpacing = 1
  64. self.scrollDirection = .vertical
  65. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  66. }
  67. required init?(coder aDecoder: NSCoder) {
  68. fatalError("init(coder:) has not been implemented")
  69. }
  70. override var itemSize: CGSize {
  71. get {
  72. if let collectionView = collectionView {
  73. self.numItems = Int(collectionView.frame.width / preferenceWidth)
  74. let itemWidth: CGFloat = (collectionView.frame.width - (marginLeftRight * 2) - CGFloat(numItems)) / CGFloat(numItems)
  75. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  76. return CGSize(width: itemWidth, height: itemHeight)
  77. }
  78. // Default fallback
  79. return CGSize(width: 100, height: 100)
  80. }
  81. set {
  82. super.itemSize = newValue
  83. }
  84. }
  85. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  86. return proposedContentOffset
  87. }
  88. }