Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. var collectionView: UICollectionView!
  2. var titleLabel: UILabel!
  3.  
  4. override func viewDidLoad() {
  5. super.viewDidLoad()
  6. // Do any additional setup after loading the view, typically from a nib.
  7. self.view.translatesAutoresizingMaskIntoConstraints = false
  8. let margins = self.view.layoutMarginsGuide
  9.  
  10. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  11. layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
  12.  
  13. collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
  14. layout.itemSize = CGSize(width: self.collectionView.frame.width / 4, height: self.collectionView.frame.width / 4)
  15. layout.minimumInteritemSpacing = self.collectionView.frame.width / 15
  16. layout.minimumLineSpacing = self.collectionView.frame.width / 5
  17.  
  18. collectionView.backgroundColor = UIColor.black
  19. collectionView.dataSource = self
  20. collectionView.delegate = self
  21. collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
  22.  
  23. titleLabel = UILabel()
  24. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  25.  
  26. titleLabel.textAlignment = .center
  27. titleLabel.numberOfLines = 1
  28. titleLabel.font = UIFont.systemFont(ofSize: 22)
  29. titleLabel.backgroundColor = UIColor.lightGray
  30. titleLabel.text = "Challenges"
  31. titleLabel.textColor = UIColor.red
  32.  
  33. let stackView = UIStackView(arrangedSubviews: [titleLabel, collectionView])
  34. stackView.backgroundColor = UIColor.white
  35. stackView.axis = .vertical
  36. stackView.distribution = .fillEqually
  37. stackView.alignment = .fill
  38. stackView.spacing = 5
  39. self.view.addSubview(stackView)
  40. stackView.translatesAutoresizingMaskIntoConstraints = false
  41.  
  42. //stackView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
  43. //stackView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
  44. stackView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
  45. stackView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
  46. stackView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
  47. stackView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
  48. stackView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1.0).isActive = true
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement