Skip to content

Commit

Permalink
Merge pull request #37 from boostcampwm-2024/feature/#35-anchor
Browse files Browse the repository at this point in the history
feat: 오토레이아웃 편의 Anchor 함수 작성
  • Loading branch information
Kyxxn authored Nov 9, 2024
2 parents e46467c + d06b420 commit 5714c3f
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 15 deletions.
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 @@ final class CustomAlbumViewController: UIViewController {
flowLayout.minimumInteritemSpacing = 5
flowLayout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
collectionView.translatesAutoresizingMaskIntoConstraints = false

return collectionView
}()
Expand Down Expand Up @@ -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
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)
}

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
)
}
}

0 comments on commit 5714c3f

Please sign in to comment.