Last active
February 25, 2019 05:41
-
-
Save moraei/08f1c1841f7bb73bb5c9e89ac428e027 to your computer and use it in GitHub Desktop.
Make a stack view scrollable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(scrollView) | |
scrollView.addSubview(scrollViewContainer) | |
scrollViewContainer.addArrangedSubview(redView) | |
scrollViewContainer.addArrangedSubview(blueView) | |
scrollViewContainer.addArrangedSubview(greenView) | |
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true | |
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true | |
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true | |
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true | |
scrollViewContainer.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true | |
scrollViewContainer.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true | |
scrollViewContainer.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true | |
scrollViewContainer.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true | |
// this is important for scrolling | |
scrollViewContainer.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true | |
} | |
let scrollView: UIScrollView = { | |
let scrollView = UIScrollView() | |
scrollView.translatesAutoresizingMaskIntoConstraints = false | |
return scrollView | |
}() | |
let scrollViewContainer: UIStackView = { | |
let view = UIStackView() | |
view.axis = .vertical | |
view.spacing = 10 | |
view.translatesAutoresizingMaskIntoConstraints = false | |
return view | |
}() | |
let redView: UIView = { | |
let view = UIView() | |
view.heightAnchor.constraint(equalToConstant: 500).isActive = true | |
view.backgroundColor = .red | |
return view | |
}() | |
let blueView: UIView = { | |
let view = UIView() | |
view.heightAnchor.constraint(equalToConstant: 200).isActive = true | |
view.backgroundColor = .blue | |
return view | |
}() | |
let greenView: UIView = { | |
let view = UIView() | |
view.heightAnchor.constraint(equalToConstant: 1200).isActive = true | |
view.backgroundColor = .green | |
return view | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment