Skip to content

Instantly share code, notes, and snippets.

@KyleGoslan
Created November 9, 2020 18:42
Show Gist options
  • Save KyleGoslan/8171e1c908ac5ce6677551dcbb403c40 to your computer and use it in GitHub Desktop.
Save KyleGoslan/8171e1c908ac5ce6677551dcbb403c40 to your computer and use it in GitHub Desktop.
SnapKit Scroll View Example
//
// SampleSaleArticleViewController.swift
// aSample
//
// Created by Matthew hammond on 09/11/2020.
//
import UIKit
import Buy
import SnapKit
class SampleSaleArticleViewController: UIViewController {
//Scroll View
let scrollView = UIScrollView()
// IMPORTANT: Add all views "in the scroll view" to this view NOT the scroll view.
let scrollContentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
scrollView.addSubview(scrollContentView)
scrollContentView.snp.makeConstraints { make in
make.top.bottom.equalTo(scrollView)
make.left.right.equalTo(view) // Making the content view of the scroll view the same as teh view controllers view.
}
setupContainer()
}
func setupContainer() {
let label = UILabel()
label.h1("Title Text")
scrollContentView.addSubview(label)
let textView = UILabel()
textView.numberOfLines = 0 // Enable multiline for UILabel if you didnt know you could do this.
textView.text = "Aspinal of London sample sale take up to 80% off RRP on luxury leather goods and accessories."
scrollContentView.addSubview(textView)
label.snp.makeConstraints { make in
make.top.left.right.equalToSuperview().inset(50) // So these layouts are to the scrollContentView
}
textView.snp.makeConstraints { make in
make.top.equalTo(label.snp.bottom)
make.left.right.equalToSuperview().inset(50)
make.height.equalTo(4000) // Just some dummy height so we get some scrolling action, but this will be calculated as needed when you add more views
make.bottom.equalTo(scrollView.snp.bottom) // THIS IS THE IMPORTANT CONSTRAINT
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment