✅ 이번 시간에는 CollectionView 코드로 구성하기를 알아보자.
어렵진 않았는데, collectionView쪽에 레이아웃을 return을 하지 않아서 에러를 한참동안 ㅠㅠ
Then을 적용할 수 있는 다른 방법이 있는지도 알아봐야겠다.
✅ 코드리뷰
//
// File.swift
// SnapKit_practice
//
// Created by Hamlit Jason on 2021/08/21.
//
import UIKit
import RxDataSources
import RxSwift
import RxCocoa
import CoreLocation
import Foundation
import Differentiator
import Then
class ViewController08 : UIViewController {
var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.scrollDirection = .vertical
layout.sectionInset = .zero
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .green
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.delegate = self
collectionView.dataSource = self
autoLayout()
//collectionView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width - 100, height: self.view.frame.height - 100 )
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}
}
extension ViewController08 : UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = .blue
return cell
}
}
extension ViewController08 {
private func autoLayout() {
collectionView.snp.makeConstraints {
$0.edges.equalTo(view.safeAreaLayoutGuide)
.inset(UIEdgeInsets(top: 0, left: 0, bottom: 200, right: 0))
}
}
}
✅ 이전 포스팅
2021.08.21 - [iOS/RxSwift] - iOS RxSwift 07 | RxDataSources (Simple) - 커스텀 셀
'deprecated > iOS Third Party' 카테고리의 다른 글
iOS Snapkit 10 | CollectionView 코드로 구성하는 법 03 (0) | 2021.08.23 |
---|---|
iOS Snapkit 09 | CollectionView 코드로 구성하는 법 02 (0) | 2021.08.22 |
iOS SnapKit 05 | iOS tableView를 코드로 구성하는 법 03 (0) | 2021.08.19 |
iOS SnapKit 04 | iOS tableView를 코드로 구성하는 법 02 (0) | 2021.08.19 |
iOS SnapKit 03 | iOS tableView를 코드로 구성하는 법 01 (0) | 2021.08.19 |