Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 오토레이아웃 편의 Anchor 함수 작성 #37

Merged
merged 4 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0E5C13C62CDE8D1D005406B3 /* UIView+Anchor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E5C13C52CDE8D19005406B3 /* UIView+Anchor.swift */; };
0E7F29192CDB52DE007D4F2B /* UIFont+Ownglyph.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E7F29182CDB52D4007D4F2B /* UIFont+Ownglyph.swift */; };
0E7F291B2CDB530E007D4F2B /* UILabel+Style.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E7F291A2CDB5302007D4F2B /* UILabel+Style.swift */; };
0E7F291D2CDB53B5007D4F2B /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0E7F291C2CDB53B5007D4F2B /* Colors.xcassets */; };
Expand All @@ -22,6 +23,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0E5C13C52CDE8D19005406B3 /* UIView+Anchor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Anchor.swift"; sourceTree = "<group>"; };
0E7F28EC2CDA6C06007D4F2B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0E7F29182CDB52D4007D4F2B /* UIFont+Ownglyph.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+Ownglyph.swift"; sourceTree = "<group>"; };
0E7F291A2CDB5302007D4F2B /* UILabel+Style.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UILabel+Style.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -80,6 +82,7 @@
0E7F29172CDB52CC007D4F2B /* Extensions */ = {
isa = PBXGroup;
children = (
0E5C13C52CDE8D19005406B3 /* UIView+Anchor.swift */,
0E7F291A2CDB5302007D4F2B /* UILabel+Style.swift */,
0E7F29182CDB52D4007D4F2B /* UIFont+Ownglyph.swift */,
A840E5922CDE266F002A1C94 /* UICollectionViewCell+Identifier.swift */,
Expand Down Expand Up @@ -247,6 +250,7 @@
A8A32C532CDBB9980078B4F6 /* CustomAlbumViewController.swift in Sources */,
A840E5932CDE266F002A1C94 /* UICollectionViewCell+Identifier.swift in Sources */,
0E7F291B2CDB530E007D4F2B /* UILabel+Style.swift in Sources */,
0E5C13C62CDE8D1D005406B3 /* UIView+Anchor.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ final class CustomAlbumCollectionViewCell: UICollectionViewCell {
// MARK: - Properties
private let photoImageView: UIImageView = {
let imageView = UIImageView(image: nil)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true

Expand Down Expand Up @@ -40,12 +39,7 @@ final class CustomAlbumCollectionViewCell: UICollectionViewCell {

private func configureConstraints() {
contentView.addSubview(photoImageView)
NSLayoutConstraint.activate([
photoImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
photoImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
photoImageView.topAnchor.constraint(equalTo: contentView.topAnchor),
photoImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
photoImageView.fillSuperview()
}

// MARK: - Set Cell Image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
flowLayout.minimumInteritemSpacing = 5
flowLayout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
collectionView.translatesAutoresizingMaskIntoConstraints = false

return collectionView
}()
Expand Down Expand Up @@ -45,22 +44,16 @@

private func configureConstraints() {
view.addSubview(albumCollectionView)

NSLayoutConstraint.activate([
albumCollectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
albumCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
albumCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
albumCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
albumCollectionView.fillSuperview()
}

// MARK: - Open Camera
private func openCamera() {
if (UIImagePickerController.isSourceTypeAvailable(.camera)) {

Check warning on line 52 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Control Statement Violation: `if`, `for`, `guard`, `switch`, `while`, and `catch` statements shouldn't unnecessarily wrap their conditionals or arguments in parentheses (control_statement)

Check warning on line 52 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Control Statement Violation: `if`, `for`, `guard`, `switch`, `while`, and `catch` statements shouldn't unnecessarily wrap their conditionals or arguments in parentheses (control_statement)
imagePicker.sourceType = .camera
navigationController?.pushViewController(imagePicker, animated: true)
} else {
// TODO: - 카메라 접근 권한 Alert

Check warning on line 56 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Todo Violation: TODOs should be resolved (- 카메라 접근 권한 Alert) (todo)

Check warning on line 56 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Todo Violation: TODOs should be resolved (- 카메라 접근 권한 Alert) (todo)
}
}
}
Expand All @@ -80,8 +73,8 @@
targetSize: .zero,
contentMode: .default,
options: nil
) { image, _ in

Check warning on line 76 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Unused Closure Parameter Violation: Unused parameter in a closure should be replaced with _ (unused_closure_parameter)

Check warning on line 76 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Unused Closure Parameter Violation: Unused parameter in a closure should be replaced with _ (unused_closure_parameter)
// TODO: - 이미지 편집 뷰 로 이동

Check warning on line 77 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Todo Violation: TODOs should be resolved (- 이미지 편집 뷰 로 이동) (todo)

Check warning on line 77 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Todo Violation: TODOs should be resolved (- 이미지 편집 뷰 로 이동) (todo)
}
}
}
Expand Down Expand Up @@ -129,10 +122,10 @@
extension CustomAlbumViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]

Check warning on line 125 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)

Check warning on line 125 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
// TODO: - 이미지 편집 뷰로 이동

Check warning on line 128 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Todo Violation: TODOs should be resolved (- 이미지 편집 뷰로 이동) (todo)

Check warning on line 128 in MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Todo Violation: TODOs should be resolved (- 이미지 편집 뷰로 이동) (todo)
}
dismiss(animated: true, completion: nil)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import UIKit

extension UIView {
func setAnchor(
top: NSLayoutYAxisAnchor? = nil,
constantTop: CGFloat = 0,
leading: NSLayoutXAxisAnchor? = nil,
constantLeading: CGFloat = 0,
bottom: NSLayoutYAxisAnchor? = nil,
constantBottom: CGFloat = 0,
trailing: NSLayoutXAxisAnchor? = nil,
constantTrailing: CGFloat = 0,
width: CGFloat? = nil,
height: CGFloat? = nil
) {
self.translatesAutoresizingMaskIntoConstraints = false
if let top {
self.topAnchor.constraint(equalTo: top, constant: constantTop).isActive = true
}

if let leading {
self.leadingAnchor.constraint(equalTo: leading, constant: constantLeading).isActive = true
}

if let bottom {
self.bottomAnchor.constraint(equalTo: bottom, constant: -constantBottom).isActive = true
}

if let trailing {
self.trailingAnchor.constraint(equalTo: trailing, constant: -constantTrailing).isActive = true
}

if let width {
self.widthAnchor.constraint(equalToConstant: width).isActive = true
}

if let height {
self.heightAnchor.constraint(equalToConstant: height).isActive = true
}
}

func setTop(anchor: NSLayoutYAxisAnchor, constant: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.topAnchor.constraint(equalTo: anchor, constant: constant).isActive = true
}

func setLeading(anchor: NSLayoutXAxisAnchor, constant: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.leadingAnchor.constraint(equalTo: anchor, constant: constant).isActive = true
}

func setBottom(anchor: NSLayoutYAxisAnchor, constant: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.bottomAnchor.constraint(equalTo: anchor, constant: -constant).isActive = true
}

func setTrailing(anchor: NSLayoutXAxisAnchor, constant: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.trailingAnchor.constraint(equalTo: anchor, constant: -constant).isActive = true
}

func setCenter(view: UIView, offset: CGPoint) {
self.translatesAutoresizingMaskIntoConstraints = false
self.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: offset.x).isActive = true
self.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset.y).isActive = true
}

func setHorizontal(view: UIView, constant: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.setLeading(anchor: view.leadingAnchor, constant: constant)
self.setTrailing(anchor: view.trailingAnchor, constant: constant)
}

func setHorizontal(layoutGuide: UILayoutGuide, constant: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.setLeading(anchor: layoutGuide.leadingAnchor, constant: constant)
self.setTrailing(anchor: layoutGuide.trailingAnchor, constant: constant)
}
Comment on lines +68 to +78
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: 위 두 함수는 각각 어떻게 다른건가요?? 레이아웃 가이드만 넣어야하고, UIView만 넣어야하는 경우가 각각 따로 있나요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아래 코드를 의도하고 만들긴 했습니다 !
layoutGuide의 경우 불 필요할까요 ??

titleLabel.setVertical(layoutGuide: view.safeAreaLayoutGuide, constant: 20)
or
titleLabel.setVertical(view: view, constant: 20)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

옹 저런 경우가 있을 수 있겠군요 .ᐟ.ᐟ 저는 저런 이유라면 남겨도 될 것 같다는 의견입니다 .ᐟ.ᐟ


func setVertical(view: UIView, constant: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.setTop(anchor: view.topAnchor, constant: constant)
self.setBottom(anchor: view.bottomAnchor, constant: constant)
}

func setVertical(layoutGuide: UILayoutGuide, constant: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.setTop(anchor: layoutGuide.topAnchor, constant: constant)
self.setBottom(anchor: layoutGuide.bottomAnchor, constant: constant)
}

func setCenterX(view: UIView, constant: CGFloat = 0) {
self.translatesAutoresizingMaskIntoConstraints = false
self.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: constant).isActive = true
}

func setCenterY(view: UIView, constant: CGFloat = 0) {
self.translatesAutoresizingMaskIntoConstraints = false
self.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: constant).isActive = true
}

func setWidthAndHeight(width: CGFloat, height: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.widthAnchor.constraint(equalToConstant: width).isActive = true
self.heightAnchor.constraint(equalToConstant: height).isActive = true
}

func setHeight(_ height: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.heightAnchor.constraint(equalToConstant: height).isActive = true
}

func setWidth(_ width: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
self.widthAnchor.constraint(equalToConstant: width).isActive = true
}

func fillSuperview() {
self.translatesAutoresizingMaskIntoConstraints = false
guard let view = self.superview else { return }
self.setAnchor(
top: view.safeAreaLayoutGuide.topAnchor,
leading: view.leadingAnchor,
bottom: view.safeAreaLayoutGuide.bottomAnchor,
trailing: view.trailingAnchor
Comment on lines +122 to +125
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

top과 bottom은 safeAreaLayoutGuide로 했습니당
괜찮을까요 ??

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋아욤!

)
}
}
Loading