๋ง์ผ์ปฌ๋ฆฌ ํด๋ก ์ฝ๋ฉ ์ค์ ๊ตฌํํด์ผ ๋์ด์
StickyHeader๋ ์คํฌ๋กค์ ์๋๋ก ๋ด๋ ธ์ ๋ ์์ชฝ์ ์๋ณ์ด ๋ถ์ด์๋ ๊ฒ ์ฒ๋ผ ๋ณด์ด๋ ํ๋ฉด์ ๋งํ๋ค.
๋๋ collectionView ์์ ์คํฐํค ํค๋๋ฅผ ๋ฃ๊ณ ์ถ์๊ณ ๊ทธ๋์ ์๋์ ๊ฐ์ ์์๋ก ๊ตฌํํ๋ค.
๋ด๊ฐ ์๋จ์ ๋ถํ๋ ๋ทฐ๋ฅผ
HomeNewStickcyHeaderView
๋ผ๋ ํ์ผ์ ์์ฑํ์ฌ ๊ตฌํํด๋์๋ค.
import UIKit
import SnapKit
import Then
class HomeNewStikcyHeaderView: UIView {
private let countLabel = UILabel()
private lazy var categoryButton = UIButton()
private lazy var filterButton = UIButton()
override init(frame: CGRect) {
super.init(frame: frame)
style()
hierarchy()
layout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func style() {
self.backgroundColor = .white
countLabel.do {
$0.text = "์ด 173๊ฐ"
}
categoryButton.do {
$0.setTitle("์ ์ํ์", for: .normal)
$0.setTitleColor(.black, for: .normal)
}
filterButton.do {
$0.setTitle("ํํฐ", for: .normal)
$0.setTitleColor(.black, for: .normal)
}
}
private func hierarchy() {
self.addSubviews(countLabel,categoryButton, filterButton)
}
private func layout() {
countLabel.snp.makeConstraints {
$0.top.equalToSuperview()
$0.leading.equalToSuperview().offset(15)
$0.width.equalTo(70)
$0.height.equalTo(40)
}
categoryButton.snp.makeConstraints {
$0.top.equalToSuperview()
$0.trailing.equalToSuperview().inset(85)
$0.width.equalTo(70)
$0.height.equalTo(40)
}
filterButton.snp.makeConstraints {
$0.top.equalToSuperview()
$0.trailing.equalToSuperview().inset(15)
$0.width.equalTo(70)
$0.height.equalTo(40)
}
}
}
์คํฐํค ๋ทฐ๋ ์คํฌ๋กค์ ์ํฅ์ ๋ฐ์ผ๋ฉด ์๋๊ธฐ ๋๋ฌธ์, ๋ด๊ฐ ๊ธฐ์กด์ ๋ง๋ค์ด๋ collectionView์ ์ถ๊ฐํ๋ ๊ฒ์ด ์๋, ์์ ๋ทฐ์ ์ถ๊ฐ๋ฅผ ํด๋์๋ค.
private func hierarchy() {
self.addSubviews(homeNewCollectionView, homeNewStikcyHeaderView)
}
/*
homeNewCollecionView.addSubview(homeNewStikcyHeaderView) ์ด๋ ๊ฒ ์ํจ!
*/
์คํค๋ทฐ๋ ์๋จ์ ๊ณ ์ ๋์ด์ผ ํ๋ค. ๋ฐ๋ผ์ Snapkit์ผ๋ก ์คํ ๋ ์ด์์์ ์ก์๋ top์ ์๋จ์ ์ก์๋๊ณ , ๋์ด๋ ๊ธฐ์กด์ ๋ถ์ด๋ ค๊ณ ํ๋ ๋ทฐ์ ๊ธธ์ด์ ๋์ผํ๊ฒ ๋ ์ด์์์ ์ก๋๋ค.