Swift 인스타그램 앱 클론 코딩(4)
2022. 5. 5. 23:32ㆍSwift
반응형
Swift 인스타그램 앱 클론 코딩(4)
Swift 인스타그램 앱 클론 코딩(3)
Swift 인스타그램 앱 클론 코딩(3) Swift 인스타그램 앱 클론 코딩(2) Swift 인스타그램 UI 클론 코딩(2) Swift 인스타그램 UI 클론 코딩(1) Swift 인스타그램UI 클론 코딩 프로젝트 생성 프로젝트를 생성
wlqkr.tistory.com
ProfileViewController
- 프로필 탭을 만들기 위해 ProfileViewController 파일을 만들겠습니다.
- TabBarContoroller에서 NavigationBar를 임베디드 해주고 메서드를 정의하겠습니다.
import UIKit
import SnapKit
final class ProfileViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationItems()
}
}
private extension ProfileViewController {
func setupNavigationItems() {
navigationItem.title = "UserName"
let rightBarButton = UIBarButtonItem(
image: UIImage(systemName: "ellipsis"),
style: .plain,
target: self,
action: nil
)
navigationItem.rightBarButtonItem = rightBarButton
}
}
- Label들과 Button들을 만들어주겠습니다.
private lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.layer.cornerRadius = 40.0
imageView.layer.borderWidth = 1
imageView.layer.borderColor = UIColor.quaternaryLabel.cgColor // 옅은 회색
return imageView
}()
private lazy var nameLabel: UILabel = {
let label = UILabel()
label.text = "UserName"
label.font = .systemFont(ofSize: 14.0, weight: .semibold)
return label
}()
private lazy var desciptionLabel: UILabel = {
let label = UILabel()
label.text = "화가나세요"
label.font = .systemFont(ofSize: 14.0, weight: .medium)
label.numberOfLines = 0
return label
}()
private lazy var followButton: UIButton = {
let button = UIButton()
button.setTitle("팔로우", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 14.0, weight: .semibold)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = 3.0
return button
}()
private lazy var messageButton: UIButton = {
let button = UIButton()
button.setTitle("메시지", for: .normal)
button.setTitleColor(.label, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 14.0, weight: .semibold)
button.backgroundColor = .white
button.layer.cornerRadius = 3.0
button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.tertiaryLabel.cgColor
return button
}()
setupLayout
- 전달해줄 메서드를 하나 정의 하겠습니다.
- 팔로우 버튼과 메시지버튼은 사용하기 편하게 스택뷰로 감싸주겠습니다.
func setupLayout() {
let buttonStackView = UIStackView(arrangedSubviews: [followButton, messageButton])
buttonStackView.spacing = 4.0
buttonStackView.distribution = .fillEqually
[
profileImageView, nameLabel, desciptionLabel, buttonStackView
].forEach {view.addSubview($0) }
}
- SnapKit으로 오토레이아웃도 잡아주겠습니다.
let inset: CGFloat = 16.0
profileImageView.snp.makeConstraints {
$0.top.equalTo(view.safeAreaLayoutGuide).inset(inset)
$0.leading.equalToSuperview().inset(inset)
$0.width.equalTo(80.0)
$0.height.equalTo(profileImageView.snp.width)
}
nameLabel.snp.makeConstraints {
$0.top.equalTo(profileImageView.snp.bottom).offset(12.0)
$0.leading.equalTo(profileImageView.snp.leading)
$0.trailing.equalToSuperview().inset(inset)
}
descriptionLabel.snp.makeConstraints {
$0.top.equalTo(nameLabel.snp.bottom).offset(12.0)
$0.leading.equalTo(nameLabel.snp.leading)
$0.trailing.equalTo(nameLabel.snp.trailing)
}
buttonStackView.snp.makeConstraints {
$0.top.equalTo(descriptionLabel.snp.bottom).offset(12.0)
$0.leading.equalTo(nameLabel.snp.leading)
$0.trailing.equalTo(nameLabel.snp.trailing)
}
- viewDidLoad에 넣어주고 실행하면 잘나옵니다.
ProfileDataView
- 프로필에 표시될 ProfileDataView를 따로 만들어주겠습니다.
import UIKit
import SnapKit
final class ProfileDataView: UIView {
private let title: String
private let count: Int
private lazy var titleLable: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 14.0, weight: .medium)
label.text = title
return label
}()
private lazy var countLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 16.0, weight: .bold)
label.text = "\(count)"
return label
}()
init(title: String, count: Int) {
self.title = title
self.count = count
super.init(frame: .zero)
setupLayout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension ProfileDataView {
func setupLayout() {
let stackView = UIStackView(arrangedSubviews: [countLabel, titleLable])
stackView.axis = .vertical
stackView.alignment = .center
stackView.spacing = 4.0
addSubview(stackView)
stackView.snp.makeConstraints{ $0.edges.equalToSuperview() }
}
}
- ProfileViewController에서 값을 가져올 수 있게 코드를 수정하겠습니다.
private let photoDataView = ProfileDataView(title: "게시물", count: 1)
private let followerDataView = ProfileDataView(title: "팔로워", count: 999)
private let followingDataView = ProfileDataView(title: "팔로잉", count: 1)
func setupLayout() {
let dataStackView = UIStackView(arrangedSubviews: [photoDataView, followerDataView, followingDataView])
dataStackView.spacing = 4.0
dataStackView.distribution = .fillEqually
dataStackView.snp.makeConstraints {
$0.leading.equalTo(profileImageView.snp.trailing).offset(inset)
$0.trailing.equalToSuperview().inset(inset)
$0.centerY.equalTo(profileImageView.snp.centerY) // 중앙 정렬
}
}
- 실행을 하면 값이 잘 표시되는걸 확인할 수 있습니다.
Custom Cell
- 하단의 리스트들을 나타내 줄 UICollectionView로 Custom Cell을 만들겠습니다.
- 먼저 콜렉션뷰를 ProfileViewController에서 변수로 만들어 주겠습니다.
private lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = .systemBackground
collectionView.register(UICollectionView.self, forCellWithReuseIdentifier: "ProfileCollectionViewCell")
return collectionView
}()
- DataSource
extension ProfileViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfileCollectionViewCell", for: indexPath)
cell.backgroundColor = .lightGray
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
}
- Delegate
extension ProfileViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width: CGFloat = (collectionView.frame.width / 3) - 1.0
return CGSize(width: width, height: width)
}
}
ProfileCollectionViewCell
- 이제 Cell을 만들어주겠습니다.
import UIKit
import SnapKit
final class ProfileCollectionViewCell: UICollectionViewCell {
private let imageView = UIImageView()
func setup(with image: UIImage) {
addSubview(imageView)
imageView.snp.makeConstraints { $0.edges.equalToSuperview() }
imageView.backgroundColor = .tertiaryLabel
}
}
- ProfileViewController에 register와 cellForItemAt메서드를 수정해줍니다.
UIAlertController
- 버튼을 탭하면 수행할 수 있는 Class입니다.
- https://developer.apple.com/documentation/uikit/uialertaction/
@objc func didTapRightBarButtonItem() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
[
UIAlertAction(title: "회원 정보 변경", style: .default),
UIAlertAction(title: "탈퇴하기", style: .destructive),
UIAlertAction(title: "닫기", style: .cancel)
].forEach { actionSheet.addAction($0) }
present(actionSheet, animated: true)
}
- rightBarButton에 추가를 해줍니다.
action: #selector(didTapRightBarButtonItem)
- 프로필 화면이 완성이 됬습니다.
반응형
'Swift' 카테고리의 다른 글
Swift 인스타그램 앱 클론 코딩(6) (0) | 2022.05.08 |
---|---|
Swift 인스타그램 앱 클론 코딩(5) (0) | 2022.05.07 |
Swift 인스타그램 앱 클론 코딩(3) (0) | 2022.05.04 |
Swift 인스타그램 앱 클론 코딩(2) (0) | 2022.05.03 |
Swift 인스타그램 앱 클론 코딩(1) (0) | 2022.05.02 |