NCEmptyDataSet.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // NCEmptyDataSet.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. class NCEmptyDataSet: NSObject {
  10. var emptyView: NCEmptyView?
  11. var collectionView: UICollectionView?
  12. init(collectionView: UICollectionView, image: UIImage?, title: String, description: String) {
  13. super.init()
  14. self.collectionView = collectionView
  15. if let emptyView = UINib(nibName: "NCEmptyView", bundle: nil).instantiate(withOwner: self, options: nil).first as? NCEmptyView {
  16. self.emptyView = emptyView
  17. emptyView.frame = CGRect(x:0, y: 0, width:300, height:300)
  18. emptyView.isHidden = true
  19. emptyView.translatesAutoresizingMaskIntoConstraints = false
  20. emptyView.emptyImage.image = image
  21. emptyView.emptyTtle.text = NSLocalizedString(title, comment: "")
  22. emptyView.emptyDescription.text = NSLocalizedString(description, comment: "")
  23. collectionView.addSubview(emptyView)
  24. let constantY: CGFloat = (collectionView.frame.height - emptyView.frame.height) / 2 - 100
  25. emptyView.centerXAnchor.constraint(equalTo: collectionView.centerXAnchor).isActive = true
  26. emptyView.topAnchor.constraint(equalTo: collectionView.topAnchor, constant: constantY).isActive = true
  27. //emptyView.layoutIfNeeded()
  28. }
  29. }
  30. func numberOfItemsInSection(_ numberItems: Int) {
  31. if numberItems == 0 {
  32. emptyView?.isHidden = false
  33. } else {
  34. emptyView?.isHidden = true
  35. }
  36. }
  37. }
  38. class NCEmptyView: UIView {
  39. @IBOutlet weak var emptyImage: UIImageView!
  40. @IBOutlet weak var emptyTtle: UILabel!
  41. @IBOutlet weak var emptyDescription: UILabel!
  42. }