iOS SnapKit 공식문서로 공부하기 2탄 (offset, inset)
이번에는 constant를 offset, inset을 사용해 적용해보자
샘플코드
import UIKit
import SnapKit
final class ViewController: UIViewController {
privat elazy var box = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(box)
box.backgroundColor = .green
box.snp.makeConstraints { (make) -> Void in
make.top.equalTo(view).offset(20)
make.left.equalTo(view).offset(20)
make.bottom.equalTo(view).offset(-20)
make.right.equalTo(view).offset(-20)
}
}
}
offset을 적용하는 기본적인 코드에서 유의점
- top과 left는 안쪽으로 들여써서 20의 값을 갖는 반면
- bottom과 right는 화면 안쪽으로 하기 위해서 -20의 값을 가져야 한다.
box.snp.makeConstraints { make in
make.edges.equalTo(view)
.inset(
UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
}
inset을 적용하는 기본적인 코드에서 유의점
- top과 left는 안쪽으로 들여써서 20의 값을 갖고
- bottom과 right는 화면 안쪽으로 하기 위해서 20의 값을 가져야 한다.
위에 보이는 것과 같은 코드로도 대체될 수도 있다.
- inset의 경우에는 플러터에서 학습한 내용과 동일하게 안쪽으로 준다 !

'apple > iOS, UIKit, Documentation' 카테고리의 다른 글
| iOS SnapKit 공식문서로 공부하기 4탄 (Then 라이브러리 활용) (0) | 2021.08.18 |
|---|---|
| iOS SnapKit 공식문서로 공부하기 3탄 (UI 배치하기) (0) | 2021.08.18 |
| iOS SnapKit 공식문서로 공부하기 1탄 (0) | 2021.08.18 |
| iOS 서로 다른 해상도를 가진 레이아웃 기기별 적용 안될 때 (0) | 2021.06.23 |
| image_picker crashes when picking images on simulator running on Apple Silicon M1 (0) | 2021.05.26 |