Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save morishin/e02f2ebe8cb555048355b8f207298c2e to your computer and use it in GitHub Desktop.
Save morishin/e02f2ebe8cb555048355b8f207298c2e to your computer and use it in GitHub Desktop.
Example: UIStackView in UIScrollView
import UIKit
import PlaygroundSupport
let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
scrollView.backgroundColor = .red
let stackView = UIStackView(frame: CGRect(x: 0, y: 0, width: 1000, height: 100))
stackView.backgroundColor = .gray
stackView.axis = .horizontal
stackView.spacing = 10
stackView.distribution = .fillEqually
scrollView.addSubview(stackView)
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0).isActive = true
let whiteRectView = { () -> UIView in
let view = UIView()
view.backgroundColor = .white
return view
}
(0..<10).forEach { _ in stackView.addArrangedSubview(whiteRectView()) }
PlaygroundPage.current.liveView = scrollView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment