본문 바로가기

iOS Constraint with NSLayoutConstraint

(3)
166. Constraints with Code #4 Constraints with Code #4 Align Center 사용할 Scene은 위와 같다. Label을 중앙에 배치하고, 파란색 View를 아래에 배치한다. 단, Label은 Intrinsic Size를 사용하도록 크기 제약은 추가하지 않는다. Label과 파란색 View의 간격은 10pt, 파란색 View의 높이는 5pt, 파란색 View의 너비는 Label의 너비와 같도록 제약을 추가한다. 추가된 모습은 위와 같다. NSLayoutConstraint로 제약 추가하기 func layoutWithInitializer() { label.translatesAutoresizingMaskIntoConstraints = false tealView.translatesAutoresizingMaskIntoCo..
165. Constraints with Code #3 Constraints with Code #3 Custom Header View 화면 상단에 View를 배치하고, 너비는 margin에 관계없이 모두 채울 수 있도록 제약을 추가한다. 높이는 100pt로 고정한다. Scene은 위와 같다. 추가되는 제약은 위와 같다. 이때 추가하는 대상은 Safe Area가 아닌 View로 설정해야 하고, Constrain to margins 속성은 비활성화해야 한다. 이렇게 추가하면 System UI의 영역까지 덮게 된다. 만약 System UI를 침범하지 않게 하려면 Safe Area를 기준으로 추가하면 된다. 변경하면 위와 같은 모습이 된다. // // CustomHeaderViewController.swift // Constraints with Code Practi..
164. Constraints with code #2 Constraints with code #2 top, leading, trailing, bottom // // FillParentViewController.swift // Constraints with Code Practice // // Created by Martin.Q on 2021/12/17. // import UIKit class FillParentViewController: UIViewController { @IBOutlet weak var bottomContainer: UIView! @IBOutlet weak var orangeView: UIView! override func viewDidLoad() { super.viewDidLoad() layoutWithInitializer() } } ex..