Skip to content

Instantly share code, notes, and snippets.

@yudetamago
Created May 4, 2017 14:46
Show Gist options
  • Save yudetamago/e3c511d6d955059e071fda216678edcc to your computer and use it in GitHub Desktop.
Save yudetamago/e3c511d6d955059e071fda216678edcc to your computer and use it in GitHub Desktop.
UIScrollView with SnapKit
import UIKit
import SnapKit
func setScrollView(content: UIView) {
// called in ViewController
self.edgesForExtendedLayout = []
let scrollView = UIScrollView()
let wrapper = UIView()
self.view.addSubview(scrollView)
scrollView.addSubview(wrapper)
wrapper.addSubview(content)
content.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
scrollView.snp.makeConstraints { make in
make.top.equalTo(topLayoutGuide.snp.bottom)
make.bottom.equalToSuperview()
make.width.equalToSuperview()
}
wrapper.snp.makeConstraints { make in
make.edges.equalToSuperview()
// only vertical scroll
make.width.equalToSuperview()
// only horizontal scroll
//make.height.equalToSuperview()
}
}
@source-creator
Copy link

Why do you need wrapper? Can't you add the content view directly to scrollView?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment