Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created April 14, 2017 18:19
Show Gist options
  • Select an option

  • Save DonMag/3b07606295fa32da8f82a9b13c34c578 to your computer and use it in GitHub Desktop.

Select an option

Save DonMag/3b07606295fa32da8f82a9b13c34c578 to your computer and use it in GitHub Desktop.
import UIKit
import SnapKit
private let insets = UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10)
private let labelSpacing: CGFloat = 7
private let formPadding: CGFloat = 10
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout {
static let itemsPerRow = 5
static let padding: CGFloat = 4
private let collectionView: UICollectionView
private let layout = UICollectionViewFlowLayout()
init() {
layout.scrollDirection = .vertical
self.collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
layout.scrollDirection = .vertical
self.collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .lightGray
let scrollView = UIScrollView()
view.addSubview(scrollView)
scrollView.backgroundColor = .clear
scrollView.snp.makeConstraints { make in
make.edges.equalTo(view)
}
let contentView = UIView()
scrollView.addSubview(contentView)
contentView.backgroundColor = .clear
contentView.snp.makeConstraints { make in
make.edges.equalTo(scrollView)
make.width.equalTo(view)
}
let nameLabel = UILabel()
nameLabel.text = "Choose a name"
nameLabel.numberOfLines = 0
contentView.addSubview(nameLabel)
nameLabel.snp.makeConstraints { make in
make.top.left.right.equalTo(contentView)
}
let nameField = UITextField()
contentView.addSubview(nameField)
nameField.snp.makeConstraints { make in
make.top.equalTo(nameLabel.snp.bottom).offset(labelSpacing)
make.left.right.equalTo(contentView)
}
let nameFieldLine = UIView()
contentView.addSubview(nameFieldLine)
nameFieldLine.backgroundColor = UIColor.darkGray
nameFieldLine.snp.makeConstraints { make in
make.top.equalTo(nameField.snp.bottom)
make.left.right.equalTo(nameField)
make.height.equalTo(2)
}
let descriptionLabel = UILabel()
contentView.addSubview(descriptionLabel)
descriptionLabel.text = "Write a description"
descriptionLabel.numberOfLines = 0
descriptionLabel.snp.makeConstraints { make in
make.top.equalTo(nameFieldLine.snp.bottom).offset(formPadding)
make.left.right.equalTo(contentView)
}
let descriptionField = UITextView()
descriptionField.isScrollEnabled = false
contentView.addSubview(descriptionField)
descriptionField.textContainer.maximumNumberOfLines = 4
descriptionField.snp.makeConstraints { make in
make.top.equalTo(descriptionLabel.snp.bottom).offset(labelSpacing)
make.left.right.equalTo(contentView)
make.height.equalTo(descriptionField)
}
let isPublicLabel = UILabel()
contentView.addSubview(isPublicLabel)
isPublicLabel.numberOfLines = 0
isPublicLabel.snp.makeConstraints { make in
make.top.equalTo(descriptionField.snp.bottom).offset(formPadding)
make.left.right.equalTo(contentView)
}
let isPublicToggle = UISwitch()
contentView.addSubview(isPublicToggle)
isPublicToggle.isOn = true
isPublicToggle.snp.makeConstraints { make in
make.top.equalTo(isPublicLabel.snp.bottom).offset(10)
make.right.equalTo(contentView)
}
let isPublicInfoLabel = UILabel()
contentView.addSubview(isPublicInfoLabel)
isPublicInfoLabel.text = "Make it public"
isPublicInfoLabel.numberOfLines = 0
isPublicInfoLabel.snp.makeConstraints { make in
make.top.equalTo(isPublicLabel.snp.bottom).offset(10)
make.left.equalTo(contentView)
make.right.equalTo(isPublicToggle.snp.left).offset(-8)
}
let selectLabel = UILabel()
contentView.addSubview(selectLabel)
selectLabel.text = "Select a thing:"
selectLabel.numberOfLines = 0
selectLabel.snp.makeConstraints { make in
make.top.equalTo(isPublicInfoLabel.snp.bottom).offset(formPadding)
make.right.left.equalTo(contentView)
}
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self,
forCellWithReuseIdentifier: "cell")
collectionView.isScrollEnabled = false
collectionView.backgroundColor = .blue
contentView.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
// Probably OK
make.top.equalTo(selectLabel.snp.bottom).offset(labelSpacing)
make.left.right.equalTo(contentView)
// set height equal to 0 just so it exists...
make.height.equalTo(0)
make.bottom.equalTo(scrollView)
}
// force collectionView to "calculate" itself before view is shown
// this way, collectionView.contentSize will be valid when we hit viewWillAppear
collectionView.setNeedsLayout()
collectionView.layoutIfNeeded()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let h = collectionView.contentSize.height
collectionView.snp.updateConstraints { (make) -> Void in
make.height.equalTo(h);
}
}
// MARK: CollectionView
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 40
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemsPerRow = ViewController.itemsPerRow
let padding = ViewController.padding
let totalPaddingPerRow = CGFloat(itemsPerRow - 1) * padding + insets.left + insets.right
let width = (collectionView.bounds.width - totalPaddingPerRow) / CGFloat(ViewController.itemsPerRow)
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
// cell.itemID = 1
cell.contentView.backgroundColor = UIColor.red
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment