Advertisement
Guest User

Untitled

a guest
May 21st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
  2.  
  3. lazy var collectionView: UICollectionView = {
  4. let layout = UICollectionViewFlowLayout()
  5. let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
  6. cv.backgroundColor = .lightGray
  7. cv.dataSource = self
  8. cv.delegate = self
  9. return cv
  10. }()
  11.  
  12. let cellId = "cellId"
  13. let imageNames = ["Plan", "folder", "list", "people", "plancorrection"]
  14.  
  15. override init(frame: CGRect) {
  16. super.init(frame: frame)
  17.  
  18. collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
  19.  
  20. addSubview(collectionView)
  21. addConstraintsWithFormat("H:|[v0]|", views: collectionView)
  22. addConstraintsWithFormat("V:|[v0(200)]|", views: collectionView)
  23.  
  24. let selectedIndexPath = IndexPath(item: 0, section: 0)
  25. collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .bottom)
  26. }
  27.  
  28. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  29. return 5
  30. }
  31.  
  32. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  33. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
  34.  
  35. cell.imageView.image = UIImage(named: imageNames[indexPath.item])?.withRenderingMode(.alwaysTemplate)
  36. cell.tintColor = .white
  37.  
  38. return cell
  39. }
  40.  
  41. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  42. return CGSize(width: frame.width / 5, height: frame.height)
  43. }
  44.  
  45. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  46. return 0
  47. }
  48.  
  49. required init?(coder aDecoder: NSCoder) {
  50. fatalError("init(coder:) has not been implemented")
  51. }
  52.  
  53. }
  54.  
  55.  
  56. class MenuCell: CustomMenuBarBaseCell {
  57.  
  58. let imageView: UIImageView = {
  59. let iv = UIImageView()
  60. iv.image = UIImage(named: "Plan")?.withRenderingMode(.alwaysTemplate)
  61. iv.tintColor = .gray
  62. return iv
  63. }()
  64.  
  65. override var isHighlighted: Bool {
  66. didSet {
  67. imageView.tintColor = isHighlighted ? UIColor.white : UIColor.rgb(red: 91, green: 14, blue: 13)
  68. }
  69. }
  70.  
  71. // override var isSelected: Bool {
  72. // didSet {
  73. // imageView.tintColor = isSelected ? UIColor.white : UIColor.rgb(red: 91, green: 14, blue: 13)
  74. // }
  75. // }
  76.  
  77. override func setupViews() {
  78. super.setupViews()
  79.  
  80. addSubview(imageView)
  81.  
  82. addConstraintsWithFormat("H:[v0(28)]", views: imageView)
  83. addConstraintsWithFormat("V:[v0(28)]", views: imageView)
  84.  
  85. addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
  86. addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement