[iOS] UIScrollView
2024. 3. 9. 14:37
SnapKit으로 UIScrollView 구성
import UIKit
import SnapKit
class ViewController: UIViewController {
private lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
return scrollView
}()
private let stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.spacing = 9
return stackView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
view.addSubview(scrollView) // 스크롤 뷰 먼저 추가
// scrollview Constraints 설정
scrollView.snp.makeConstraints{
// scrollView의 top과 bottom을 safeArea로 설정
$0.top.bottom.equalTo(view.safeAreaLayoutGuide)
$0.leading.trailing.equalToSuperview()
}
// scrollView 안에 stackview 추가
scrollView.addSubview(stackView)
// stackView Constraints 설정
stackView.snp.makeConstraints{
// scrollView와 같은 width
$0.width.equalTo(scrollView.snp.width)
$0.top.equalTo(scrollView.snp.top)
$0.leading.equalTo(scrollView.snp.leading)
$0.trailing.equalTo(scrollView.snp.trailing)
$0.bottom.equalTo(scrollView.snp.bottom)
}
// StackView 안에 view 설정
let colorArray = [UIColor.blue, .red, .yellow, .purple, .green, .black, .orange, .gray]
for color in colorArray {
let elementView = UIView()
elementView.backgroundColor = color
elementView.snp.makeConstraints{
$0.height.equalTo(300) // StackView 안에 있는 View 높이 설정
}
stackView.addArrangedSubview(elementView)
}
}
}
출처
728x90
'iOS > UIKit' 카테고리의 다른 글
[iOS] UITextField PlaceHolder (0) | 2024.03.24 |
---|---|
[iOS] UICollectionViewCompositionalLayout (0) | 2024.03.09 |
[iOS] @IBInspectable (0) | 2024.01.06 |
[iOS] 키보드 처리 (0) | 2023.12.30 |
[iOS] 기종별로(iPhone, iPad) StoryBoard 구분하기 (1) | 2023.12.30 |