Search
👀

10. [No StoryBoard] SnapKit ↔ NSLayoutConstraint 대응 관계

부제
카테고리
UIKit
세부 카테고리
스토리보드 없이 개발하기
Combine 카테고리
최종편집일
2022/09/20 08:10
작성중
관련된 포스팅
생성 일시
2022/08/22 05:55
태그

사용법

1.
제약조건을 적용하기에 적절한 superView 를 선택한다.
2.
제약조건들을 추적해서 나중에 쉽게 제거하게끔 한다.
3.
translatesAutoresizingMaskIntoConstraints = false 적절한 상황에서 왼쪽의 코드가 자동으로 실행된다.
let box = UIView() superview.addSubview(box) box.snp.makeConstraints { (make) -> Void in make.top.equalTo(superview).offset(20) make.left.equalTo(superview).offset(20) make.bottom.equalTo(superview).offset(-20) make.right.equalTo(superview).offset(-20) }
Swift
복사
더 짧게는
let box = UIView() superview.addSubview(box) box.snp.makeConstraints { (make) -> Void in make.edges.equalTo(superview).inset(UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)) }
Swift
복사

equal 말고도 다양한 제약조건 사용 가능

.equalTo equivalent to NSLayoutConstraint.Relation.equal
.lessThanOrEqualTo equivalent to NSLayoutConstraint.Relation.lessThanOrEqual
.greaterThanOrEqualTo equivalent to NSLayoutConstraint.Relation.greaterThanOrEqual

ViewAttribute

ViewAttribute
NSLayoutAttribute
view.snp.left
NSLayoutConstraint.Attribute.left
view.snp.right
NSLayoutConstraint.Attribute.right
view.snp.top
NSLayoutConstraint.Attribute.top
view.snp.bottom
NSLayoutConstraint.Attribute.bottom
view.snp.leading
NSLayoutConstraint.Attribute.leading
view.snp.trailing
NSLayoutConstraint.Attribute.trailing
view.snp.width
NSLayoutConstraint.Attribute.width
view.snp.height
NSLayoutConstraint.Attribute.height
view.snp.centerX
NSLayoutConstraint.Attribute.centerX
view.snp.centerY
NSLayoutConstraint.Attribute.centerY
view.snp.lastBaseline
NSLayoutConstraint.Attribute.lastBaseline
// 아래 두 줄의 코드는 동일한 기능을 수행함. make.left.greaterThanOrEqualTo(label) make.left.greaterThanOrEqualTo(label.snp.left)
Swift
복사

몇가지 편리한 메서드들

Edges : top, left, right bottom 세트

// make top, left, bottom, right equal view2 make.edges.equalTo(view2); // make top = superview.top + 5, left = superview.left + 10, // bottom = superview.bottom - 15, right = superview.right - 20 make.edges.equalTo(superview).inset(UIEdgeInsets(top: 5, left: 10, bottom: 15, right: 20))
Swift
복사

size : 크기에 대한 제약조건을 걸고자 할 때

// make width and height greater than or equal to titleLabel make.size.greaterThanOrEqualTo(titleLabel) // make width = superview.width + 100, height = superview.height + 100 make.size.equalTo(superview).offset(100)
Swift
복사

center - 중심에 대한 제약조건을 걸고자 할 때

// make centerX and centerY = button1 make.center.equalTo(button1) // make centerX = superview.centerX + 5, centerY = superview.centerY + 5 make.center.equalTo(superview).offset(5)
Swift
복사
또한 여러가지의 view attribute 들을 가독성있고 빠르게 설정해줄 수 있다.
// All edges but the top should equal those of the superview make.left.right.bottom.equalTo(superview) make.top.equalTo(otherView)
Swift
복사

제약조건의 참조를 만들어야하는 경우.

var topConstraint: Constraint? = nil ... // when making constraints view1.snp.makeConstraints { (make) -> Void in self.topConstraint = make.top.equalTo(superview).offset(padding.top).constraint make.left.equalTo(superview).offset(padding.left) } ... // then later you can call self.topConstraint.deactivate() // or if you want to update the constraint self.topConstraint.updateOffset(5)
Swift
복사
레이아웃에 대한 애니메이션을 주고자 할 때 유용하게 사용할 수 있다.

2. snp.updateConstraints

제약조건의 constant 값만 변경하는 경우, make 대신 updateConstraints 메서드를 사용하자
// this is Apple's recommended place for adding/updating constraints // this method can get called multiple times in response to setNeedsUpdateConstraints // which can be called by UIKit internally or in your code if you need to trigger an update to your constraints override func updateConstraints() { self.growingButton.snp.updateConstraints { (make) -> Void in make.center.equalTo(self); make.width.equalTo(self.buttonSize.width).priority(250) make.height.equalTo(self.buttonSize.height).priority(250) make.width.lessThanOrEqualTo(self) make.height.lessThanOrEqualTo(self) } // according to Apple super should be called at end of method super.updateConstraints() }
Swift
복사

3. snp.remakeConstraints

해당 메서드는 SnapKit 에 의해 적용된 기존에 존재하는 모든 제약조건을 제거하고, 다시 생성한다.
func changeButtonPosition() { self.button.snp.remakeConstraints { (make) -> Void in make.size.equalTo(self.buttonSize) if topLeft { make.top.left.equalTo(10) } else { make.bottom.equalTo(self.view).offset(-10) make.right.equalTo(self.view).offset(-10) } } }
Swift
복사

디버깅

.labeled 를 사용하면 디버그 로그를 작성할 수 있다.
button.snp.makeConstraints { (make) -> Void in make.top.equalTo(otherView).labeled("buttonViewTopConstraint") } "<SnapKit.LayoutConstraint:buttonViewTopConstraint@SignUpViewController.swift#311 UIView:0x7fd98491e4c0.leading == UIView:0x7fd983633880.leading>"
Swift
복사