반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 라이트모드
- UIButton
- LazyHStack
- github
- dictionary
- autolayout
- Java
- Swift
- SwiftLint
- 웹뷰
- Alamofire
- Apple
- swipe
- UITabBarController
- 다크모드
- string
- 문자열
- NavigationLink
- Observable
- Firebase
- subscript
- remote config
- ios
- SwiftUI
- gcd
- Android
- RxSwift
- Realtime Database
- UIScrollView
- WebView
Archives
- Today
- Total
점진적 과부하 개발 블로그
Swift UIScrollView 본문
반응형
UIScrollView와 UIStackView를 이용해서 Section이 있는 CollectionView처럼 만들기
AppViewController.swift
- UIScrollView, UIStackView 프로퍼티 선언
import SnapKit
import UIKit
final class AppViewController: UIViewController {
private let scrollView = UIScrollView()
private let contentView = UIView()
private lazy var stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .equalSpacing // 간격 동일
stackView.spacing = 0.0 // SubView들이 알아서 크기 맞춤
let featureSectionView = FeatureSectionView(frame: .zero)
let rankingFeatureSectionView = UIView()
let exchangeCodeButtonView = UIView()
rankingFeatureSectionView.backgroundColor = .blue
exchangeCodeButtonView.backgroundColor = .yellow
[
featureSectionView,
rankingFeatureSectionView,
exchangeCodeButtonView
].forEach {
stackView.addArrangedSubview($0)
}
return stackView
}()
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationController()
setupLayout()
}
}
private extension AppViewController {
func setupNavigationController() {
navigationItem.title = "앱"
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true // 무슨일이 있어도 무조건 LargeTitle
}
- View들의 Layout 설정 메서드
func setupLayout() { // Layout 설정
view.addSubview(scrollView)
scrollView.snp.makeConstraints {
$0.top.equalTo(view.safeAreaLayoutGuide.snp.top)
$0.bottom.equalToSuperview()
$0.trailing.equalToSuperview()
$0.leading.equalToSuperview()
}
scrollView.addSubview(contentView)
contentView.snp.makeConstraints {
$0.edges.equalToSuperview()
$0.width.equalToSuperview() // width를 고정시켜야 가로스크롤이 아닌 세로스크롤이 됨, 반대로 Height으로 설정하면 세로
}
contentView.addSubview(stackView)
stackView.snp.makeConstraints {
$0.edges.equalToSuperview() //
}
}
FeatureSectionView.swift
- collectionView 생성
import SnapKit
import UIKit
final class FeatureSectionView: UIView {
private lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.isPagingEnabled = true
collectionView.backgroundColor = .systemBackground
collectionView.showsHorizontalScrollIndicator = false
collectionView.register(
FeatureSectionCollectionViewCell.self,
forCellWithReuseIdentifier: "FeatureSectionCollectionViewCell"
)
return collectionView
}()
override init(frame: CGRect) { // frame을 사용하는 init메서드에서만 setupViews를 호출
super.init(frame: frame)
setupViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension FeatureSectionView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeatureSectionCollectionViewCell", for: indexPath) as? FeatureSectionCollectionViewCell
cell?.setup()
return cell ?? UICollectionViewCell()
}
}
extension FeatureSectionView: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
CGSize(width: collectionView.frame.width - 32.0, height: frame.width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
UIEdgeInsets(top: 0.0, left: 16.0, bottom: 0.0, right: 16.0) // 중앙 정렬
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
32.0
}
}
private extension FeatureSectionView {
func setupViews() {
[
collectionView
].forEach { addSubview($0) }
collectionView.snp.makeConstraints {
$0.leading.equalToSuperview()
$0.trailing.equalToSuperview()
$0.top.equalToSuperview().inset(16.0)
$0.height.equalTo(snp.width)
$0.bottom.equalToSuperview()
}
}
}
FeatureSectionCollectionViewCell.swift
- Cell 설정
import SnapKit
import UIKit
final class FeatureSectionCollectionViewCell: UICollectionViewCell {
private lazy var typeLabel: UILabel = {
let label = UILabel()
label.textColor = .systemBlue
label.font = .systemFont(ofSize: 12.0, weight: .semibold)
return label
}()
private lazy var appNameLabel: UILabel = {
let label = UILabel()
label.textColor = .label
label.font = .systemFont(ofSize: 20.0, weight: .bold)
return label
}()
private lazy var descriptionLabel: UILabel = {
let label = UILabel()
label.textColor = .secondaryLabel
label.font = .systemFont(ofSize: 16.0, weight: .semibold)
return label
}()
private lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.layer.cornerRadius = 7.0
imageView.layer.borderWidth = 0.5
imageView.layer.borderColor = UIColor.separator.cgColor
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
return imageView
}()
func setup() {
setupLayout()
typeLabel.text = "test"
appNameLabel.text = "app name"
descriptionLabel.text = "description"
imageView.backgroundColor = .lightGray
}
}
private extension FeatureSectionCollectionViewCell {
func setupLayout() {
[
typeLabel,
appNameLabel,
descriptionLabel,
imageView
].forEach { addSubview($0) }
typeLabel.snp.makeConstraints {
$0.leading.equalToSuperview()
$0.trailing.equalToSuperview()
$0.top.equalToSuperview()
}
appNameLabel.snp.makeConstraints {
$0.leading.equalToSuperview()
$0.trailing.equalToSuperview()
$0.top.equalTo(typeLabel.snp.bottom)
}
descriptionLabel.snp.makeConstraints {
$0.leading.equalToSuperview()
$0.trailing.equalToSuperview()
$0.top.equalTo(appNameLabel.snp.bottom).offset(4.0)
}
imageView.snp.makeConstraints {
$0.leading.equalToSuperview()
$0.trailing.equalToSuperview()
$0.top.equalTo(descriptionLabel.snp.bottom).offset(8.0)
$0.bottom.equalToSuperview().inset(8.0)
}
}
}
반응형
'Swift' 카테고리의 다른 글
Swift plist 불러오기 (0) | 2022.04.23 |
---|---|
Swift CollectionView에서 상세화면으로 넘기기 (0) | 2022.04.22 |
Swift TabBar 화면 구현 (0) | 2022.04.12 |
Swift UITabBarController (0) | 2022.04.11 |
Swift 배너 만들기 (0) | 2022.04.10 |