Building Netflix App in Swift 5 and UIKit - (Xcode 13, 2021) - Episode 4 - Navigation Bar
class HomeViewModel {
var sectionModels: CurrentValueSubject<[HomeSectionModel], Never> = .init([])
init() {
addSubcription()
}
private func addSubcription() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: { [weak self] in
self?.sectionModels.send(self?.getMockData() ?? [])
})
}
private func getMockData() -> [HomeSectionModel] {
let mockContents1 = [ContentModel(name: "사라진 탄환", imageURLString: nil), ContentModel(name: "12 솔져스", imageURLString: nil), ContentModel(name: "듄", imageURLString: nil)]
let mockSection1 = HomeSectionModel(sectionName: "해외 액션", contents: mockContents1)
let mockContents2 = [ContentModel(name: "슈룹", imageURLString: nil), ContentModel(name: "서부 전선 이상 없다", imageURLString: nil)]
let mockSection2 = HomeSectionModel(sectionName: "지금 뜨는 콘텐츠", contents: mockContents2)
let mockContents3 = [ContentModel(name: "스파이더맨", imageURLString: nil), ContentModel(name: "무법변호사", imageURLString: nil)]
let mockSection3 = HomeSectionModel(sectionName: "최근 찾아본 콘텐츠", contents: mockContents3)
let mockContents4 = [ContentModel(name: "작은 아씨들", imageURLString: nil), ContentModel(name: "크리미널 스쿼드", imageURLString: nil)]
let mockSection4 = HomeSectionModel(sectionName: "새로 올라온 콘텐츠", contents: mockContents4)
return [mockSection1, mockSection2, mockSection3, mockSection4]
}
}
private func bind() {
viewModel
.sectionModels
.receive(on: DispatchQueue.main)
.sink { [weak self] models in
self?.tableView.reloadData()
}
.store(in: &cancellables)
}
private func setNavigationBar() {
navigationItem.largeTitleDisplayMode = .never
let leftButton = UIButton(type: .system)
leftButton.setImage(UIImage(named: "logo")?.withRenderingMode(.alwaysOriginal), for: .normal)
let leftItem = UIBarButtonItem(customView: leftButton)
leftItem.customView?.translatesAutoresizingMaskIntoConstraints = false
leftItem.customView?.heightAnchor.constraint(equalToConstant: 36).isActive = true
leftItem.customView?.widthAnchor.constraint(equalToConstant: 20).isActive = true
navigationItem.leftBarButtonItem = leftItem
let playButton = UIBarButtonItem(image: UIImage(systemName: "airplayvideo.circle")?.withTintColor(.label, renderingMode: .alwaysOriginal), style: .done, target: self, action: nil)
let profileButton = UIBarButtonItem(image: UIImage(systemName: "person")?.withTintColor(.label, renderingMode: .alwaysOriginal), style: .done, target: self, action: nil)
navigationItem.rightBarButtonItems = [
playButton,
profileButton
]
}
customView
사용)func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.textLabel?.font = .systemFont(ofSize: 18, weight: .semibold)
header.textLabel?.textColor = .white
header.textLabel?.frame = CGRect(x: header.bounds.origin.x + 20, y: header.bounds.origin.y, width: 100, height: header.bounds.height)
}
테이블 뷰 셀 내에 별도의 커스텀 컬렉션 뷰가 있는 상황이기 때문에 바인딩을 여러 차례 해줘야 함