[Review] (3주차) 실습
2022.07.01 - [Swift] - [Xcode] iOS Swift 앱 개발 Byte Degree - week.03
[Xcode] iOS Swift 앱 개발 Byte Degree - week.03
[Review] (2주차) 실습 2022.06.26 - [Swift] - [Xcode] iOS Swift 앱 개발 Byte Degree - week.02 [Xcode] iOS Swift 앱 개발 Byte Degree - week.02 iOS 아키텍처 패턴 중 애플에서는 기본적으로 MVC 패턴을 가..
sarahee.tistory.com
다크모드 변경
command + shift + a (toggle)
주석 달기
command + /
내비게이션 컨트롤러 설정
Editor > Embed in > Navigation Controller
Prefers Large Titles 체크
Safe Area 설정
Vertical에서
Align Bottom to: Safe Area / Align Top to: Safe Area 더블클릭
Fitst Item: Safe Area Bottom -> Superview, 다시 이전 Edit -> Constant: 0 설정
좌우측 여백(padding)
override func viewDidLead() { 내에
collectionView.contentInset = UIEdgeInsets(top: 20, left: 30, bottom: 0, right: 30)
탭바 컨트롤러의 주요 뷰
[Documentation] UITabBarController
https://developer.apple.com/documentation/uikit/uitabbarcontroller
File > New > File (or command + N) > Cocoa Touch Class > Class: FocusViewController, Subclass of: UIViewController
Main > Focus View Controller의 Class와 Storyboard ID에 FocusViewController 입력
UI의 Collection View 설정, 셀 디자인 및 오토레이아웃 설정
//
// FocusViewController.swift
// HeadSpaceFocus
//
// Created by sehee on 2022/07/10.
//
import UIKit
class FocusViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
var items: [Focus] = Focus.list
typealias Item = Focus
enum Section {
case main
}
var datasource: UICollectionViewDiffableDataSource<Section, Item>!
override func viewDidLoad() {
super.viewDidLoad()
// Presentation, Data, Layout
datasource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView, cellProvider: { collectionView, indexPath, item in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FocusCell", for: indexPath) as? FocusCell else {
return nil
}
cell.configure(item)
return cell
})
// data: Snapshot
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
snapshot.appendItems(items, toSection: .main)
datasource.apply(snapshot)
collectionView.collectionViewLayout = layout()
}
private func layout() -> UICollectionViewCompositionalLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(50))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(50))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
}
//
// FocusCell.swift
// HeadSpaceFocus
//
// Created by sehee on 2022/07/10.
//
import UIKit
class FocusCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var thumbnailImageView: UIImageView!
func configure(_ item: Focus) {
titleLabel.text = item.title
descriptionLabel.text = item.description
thumbnailImageView.image = UIImage(systemName: item.imageName)?.withRenderingMode(.alwaysOriginal)
}
}
FocusCell 코드 추가
override func awakeFromNib() {
super.awakeFromNib()
contentView.backgroundColor = UIColor.systemIndigo
contentView.layer.cornerRadius = 10
}
FocusViewController 코드 추가
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20)
section.interGroupSpacing = 10
FocusViewController 코드 추가
// 1
@IBOutlet weak var refreshButton: UIButton!
var curated: Bool = false
// 2
let title = curated ? "See All" : "See Recommendation"
refreshButton.setTitle(title, for: .normal)
// 3
@IBAction func refreshButtonTapped(_ sender: Any) {
curated.toggle()
self.items = curated ? Focus.recommendations : Focus.list
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
snapshot.appendItems(items, toSection: .main)
datasource.apply(snapshot)
let title = curated ? "See All" : "See Recommendation"
refreshButton.setTitle(title, for: .normal)
}
DRY 원칙(Do not Repeat Yourself)
updateButtonTitle() 함수 생성하여 중복 코드 제거
func updateButtonTitle() {
let title = curated ? "See All" : "See Recommendation"
refreshButton.setTitle(title, for: .normal)
}
버튼 둥글게 설정
refreshButton.layer.cornerRadius = 10
전체 코드
//
// FocusViewController.swift
// HeadSpaceFocus
//
// Created by sehee on 2022/07/10.
//
import UIKit
class FocusViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var refreshButton: UIButton!
var curated: Bool = false
var items: [Focus] = Focus.list
typealias Item = Focus
enum Section {
case main
}
var datasource: UICollectionViewDiffableDataSource<Section, Item>!
override func viewDidLoad() {
super.viewDidLoad()
refreshButton.layer.cornerRadius = 10
// Presentation, Data, Layout
datasource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView, cellProvider: { collectionView, indexPath, item in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FocusCell", for: indexPath) as? FocusCell else {
return nil
}
// let data = self.items[indexPath.item]
// cell.configure(data)
cell.configure(item)
return cell
})
// data: Snapshot
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
snapshot.appendItems(items, toSection: .main)
datasource.apply(snapshot)
collectionView.collectionViewLayout = layout()
updateButtonTitle()
}
private func layout() -> UICollectionViewCompositionalLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(50))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(50))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20)
section.interGroupSpacing = 10
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
func updateButtonTitle() {
let title = curated ? "See All" : "See Recommendation"
refreshButton.setTitle(title, for: .normal)
}
@IBAction func refreshButtonTapped(_ sender: Any) {
curated.toggle()
self.items = curated ? Focus.recommendations : Focus.list
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
snapshot.appendItems(items, toSection: .main)
datasource.apply(snapshot)
updateButtonTitle()
}
}
//
// FocusCell.swift
// HeadSpaceFocus
//
// Created by sehee on 2022/07/10.
//
import UIKit
class FocusCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var thumbnailImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
contentView.backgroundColor = UIColor.systemIndigo
contentView.layer.cornerRadius = 10
}
func configure(_ item: Focus) {
titleLabel.text = item.title
descriptionLabel.text = item.description
thumbnailImageView.image = UIImage(systemName: item.imageName)?.withRenderingMode(.alwaysOriginal)
}
}
[Next] (5주차) 실습
2022.07.16 - [Dev/Swift] - [Xcode] iOS Swift 앱 개발 Byte Degree - week.05
[Xcode] iOS Swift 앱 개발 Byte Degree - week.05
[Review] (4주차) 실습 2022.07.10 - [Dev/Swift] - [Xcode] iOS Swift 앱 개발 Byte Degree - week.04 [Xcode] iOS Swift 앱 개발 Byte Degree - week.04 [Review] (3주차) 실습 2022.07.01 - [Swift] - [Xcode]..
sarahee.tistory.com
'Development > Swift' 카테고리의 다른 글
[Swift] iOS 앱 개발(Xcode) Byte Degree - week.06 (0) | 2022.07.22 |
---|---|
[Swift] iOS 앱 개발(Xcode) Byte Degree - week.05 (0) | 2022.07.16 |
[Swift] iOS 앱 개발(Xcode) Byte Degree - week.03 (0) | 2022.07.01 |
[Swift] iOS 앱 개발(Xcode) Byte Degree - week.02 (0) | 2022.06.26 |
[Swift] iOS 앱 개발(Xcode) Byte Degree - week.01 (0) | 2022.06.19 |