Skip to content

Instantly share code, notes, and snippets.

@tianchu
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tianchu/b0b9d92cdda66fa37451 to your computer and use it in GitHub Desktop.
Save tianchu/b0b9d92cdda66fa37451 to your computer and use it in GitHub Desktop.
ScrollView + AutoLayout using Swift and PureLayout
class MyViewController: UIViewController {
var scrollView = UIScrollView.newAutoLayoutView()
var contentView = UIView.newAutoLayoutView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)
scrollView.addSubview(contentView)
contentView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)
contentView.autoMatchDimension(.Width, toDimension: .Width, ofView: view)
var subview1 = UILabel()
subview1.text = "Test"
contentView.addSubview(subview1)
subview1.autoPinEdgeToSuperviewEdge(.Top, withInset: 20)
subview1.autoPinEdgeToSuperviewEdge(.Leading, withInset: 20)
}
}
@thomasbaldwin
Copy link

Hey @tianchu! I'm trying to do something like this with your code, but it won't scroll. Any suggestions?

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .whiteColor()
        view.addSubview(scrollView)
        scrollView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)

        scrollView.addSubview(contentView)
        contentView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)
        contentView.autoMatchDimension(.Width, toDimension: .Width, ofView: view)

        var subview1 = UILabel()
        subview1.text = "Test"
        contentView.addSubview(subview1)

        for i in 0..<100 {
            var subview = UILabel()
            subview.text = "test"
            contentView.addSubview(subview)

            let topInset: CGFloat = CGFloat((i+1)*20)
            subview.autoPinEdgeToSuperviewEdge(.Top, withInset: topInset)
            subview.autoPinEdgeToSuperviewEdge(.Leading, withInset: 20)
        }
    }

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