Last active
October 24, 2024 03:37
-
-
Save morishin/e02f2ebe8cb555048355b8f207298c2e to your computer and use it in GitHub Desktop.
Example: UIStackView in UIScrollView
This file contains hidden or 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 | |
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