[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-09 21:08:02 +09:00
parent f0befc887a
commit 93ec7e9056
363 changed files with 68333 additions and 64 deletions
@@ -0,0 +1,98 @@
---
id: ios-uikit-autolayout-patterns
title: UIKit AutoLayout 실전 패턴
category: Coding
status: draft
source_trust_level: B
verification_status: conceptual
created_at: 2026-05-09
updated_at: 2026-05-09
tags: [ios, uikit, autolayout, layout, vibe-coding]
tech_stack: { language: "Swift / UIKit", applicable_to: ["iOS"] }
applied_in: []
aliases: [NSLayoutAnchor, priority, content hugging, compression resistance]
---
# UIKit AutoLayout 실전 패턴
> AutoLayout 의 어려움은 (1) anchor / constraint API 자체가 아니라 (2) **priority + hugging + compression resistance** 의 의미와 (3) **runtime 충돌 디버깅**.
## 📖 핵심 개념
- Constraint 는 식: `view1.attribute = m * view2.attribute + c`
- Priority 1~1000: 1000 = required, 그 외 = optional. 충돌 시 priority 높은 게 이김.
- Content hugging: "내가 늘어나기 싫어" 강도.
- Compression resistance: "내가 줄어들기 싫어" 강도.
## 💻 코드 패턴
### NSLayoutAnchor (UIKit native)
```swift
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: superview.safeAreaLayoutGuide.topAnchor, constant: 16),
view.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 16),
view.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: -16),
view.heightAnchor.constraint(equalToConstant: 44),
])
```
### Stack views (제일 자주 쓰임)
```swift
let stack = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel, button])
stack.axis = .vertical
stack.spacing = 8
stack.alignment = .fill
view.addSubview(stack)
// stack constraint
```
### Hugging / Compression — 두 label 한 줄에
```swift
//
priceLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
nameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
// name "..." , price
```
### 동적 height (multiline label)
```swift
descriptionLabel.numberOfLines = 0 //
// width , height
descriptionLabel.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -32).isActive = true
```
### 런타임 충돌 디버그
```swift
// "Will attempt to recover by breaking constraint ..."
// constraint identifier
let c = view.heightAnchor.constraint(equalToConstant: 44)
c.identifier = "RowMinHeight"
c.isActive = true
```
## 🤔 의사결정 기준
| 레이아웃 | 권장 |
|---|---|
| 정렬·간격 단순 | UIStackView |
| 복잡 비율 (golden ratio 등) | NSLayoutAnchor + multiplier |
| 외부 라이브러리 OK | SnapKit (가독성↑) |
| 동적 height (table cell) | label numberOfLines 0 + UITableView.automaticDimension |
| 안전영역 / 노치 | safeAreaLayoutGuide |
| 키보드 회피 | NSLayoutConstraint constant 변경 + animation |
## ❌ 안티패턴
- **translatesAutoresizingMaskIntoConstraints 끄기 잊음**: 자동 frame constraint 가 충돌. 먼저 false.
- **frame + autolayout 혼용**: 한 view 는 한 모델. layoutSubviews 에서 frame 조작 시 autolayout 깨짐.
- **모든 priority required**: 첫 충돌에 앱 crash.
- **stack view 안에 0 spacing 으로 또 stack**: 가독성 떨어짐. 설계 재고.
- **safeAreaLayoutGuide 무시**: 노치 / Dynamic Island 위에 그림.
- **dynamic type 무시 (큰 글씨 설정)**: label 잘림. preferredFont(forTextStyle:) + adjustsFontForContentSizeCategory.
- **다국어 RTL 무시**: leading/trailing (NOT left/right).
## 🤖 LLM 활용 힌트
- "leading/trailing 사용 (RTL 자동), top/bottom" 명시.
- 복잡 layout 은 SnapKit 또는 Stack View first.
## 🔗 관련 문서
- [[iOS_SwiftUI_State_Property_Wrappers]]
- [[iOS_SwiftUI_Lifecycle_View_Identity]]