NSLayoutConstraint

권현석·2023년 2월 21일
0

코드베이스

목록 보기
1/1

NSLayoutConstraint은 선형 방정식의 형태를 갖는다.

item1.attribute1 = multiplier X item2.attribute2 + constant

예제

두 버튼 사이의 간격이 8포인트 일 경우
=> button2.leading = 1.0 X button1.trailing + 8.0

또한 선형방정식의 성질을 통해

button1.trailing = 1.0 X button2.leading - 8.0
이 식도 위의 식과 똑같은 constraint를 만들어냄을 알 수 있다.

  • constraint는 solution이 하나만 있어야한다.
  • constraint는 (=, ≥, ≤)을 사용 할 수 있고, 1 ~ 1000의 값으로 설정 가능
  • 필수 constraint 해결 후 Auto layout은 optional constraint를 우선순위가 높은 것 부터 해결함. 이를 해결 못하면 근접한 값을 할당하고 다음 contraint으로 넘어간다.

NSLayoutConstraint 코드 사용

NSLayoutConatraint(item: button, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0)
  • parameter 알아보기
    item: constraint 지정 할 UI
    attribute: constraint 지정 할 UI의 제약속성
    relatedBy: 제약 지정할 UI와 기준이 되는 UI의 관계
    toItem: 제약 기준이 되는 UI
    attribute: 제약 기준이 되는 UI의 제약속성
    multiplier: 제약의 비율
    constraint: 제약 상수값
profile
wanna be an iOS developer

0개의 댓글