diff --git a/MemorialHouse/MHPresentation/MHPresentation.xcodeproj/project.pbxproj b/MemorialHouse/MHPresentation/MHPresentation.xcodeproj/project.pbxproj index 503ae2c7..44e451b1 100644 --- a/MemorialHouse/MHPresentation/MHPresentation.xcodeproj/project.pbxproj +++ b/MemorialHouse/MHPresentation/MHPresentation.xcodeproj/project.pbxproj @@ -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 */; }; @@ -22,6 +23,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 0E5C13C52CDE8D19005406B3 /* UIView+Anchor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Anchor.swift"; sourceTree = ""; }; 0E7F28EC2CDA6C06007D4F2B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 0E7F29182CDB52D4007D4F2B /* UIFont+Ownglyph.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+Ownglyph.swift"; sourceTree = ""; }; 0E7F291A2CDB5302007D4F2B /* UILabel+Style.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UILabel+Style.swift"; sourceTree = ""; }; @@ -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 */, @@ -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; }; diff --git a/MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumCollectionViewCell.swift b/MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumCollectionViewCell.swift index f8506a17..98c0969e 100644 --- a/MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumCollectionViewCell.swift +++ b/MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumCollectionViewCell.swift @@ -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 @@ -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 diff --git a/MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift b/MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift index ddfa62a5..f56995ad 100644 --- a/MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift +++ b/MemorialHouse/MHPresentation/MHPresentation/Source/CustomAlbum/CustomAlbumViewController.swift @@ -15,7 +15,6 @@ final class CustomAlbumViewController: UIViewController { flowLayout.minimumInteritemSpacing = 5 flowLayout.scrollDirection = .vertical let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) - collectionView.translatesAutoresizingMaskIntoConstraints = false return collectionView }() @@ -45,13 +44,7 @@ final class CustomAlbumViewController: UIViewController { 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 diff --git a/MemorialHouse/MHPresentation/MHPresentation/Source/Extensions/UIView+Anchor.swift b/MemorialHouse/MHPresentation/MHPresentation/Source/Extensions/UIView+Anchor.swift new file mode 100644 index 00000000..07274f0f --- /dev/null +++ b/MemorialHouse/MHPresentation/MHPresentation/Source/Extensions/UIView+Anchor.swift @@ -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) + } + + 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 + ) + } +}