longlivedrgn
Miro 찾기
longlivedrgn
전체 방문자
오늘
어제
  • 분류 전체보기 (74)
    • Swift (36)
    • iOS (31)
    • Algorithm (0)
    • Architecture, Design Patter.. (1)
    • Computer Science (6)
      • 컴퓨터 네트워크 (6)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
longlivedrgn

Miro 찾기

[iOS] Priority(Autolayout)
iOS

[iOS] Priority(Autolayout)

2023. 2. 3. 21:26

Priority

  • UILayoutPriority 는 0~1000의 값을 가진다.
  • .required == 1000 .defaultHigh == 750 .defaultLow == 250
  • lessThanOrEqualTo 나 greaterThanOrEqualTo 는 다른 제한조건과 같이 쓰인다. 만약 다른 제한조건이 없다면 equal로 간다!
  • 아래의 경우, 아이폰 X과 SE(8)의 safe area의 차이를 보여주는 그림이다.
  • SE는 safe area의 bottom과 super view의 bottom이 같음을 알 수 있다.
  • https://velog.velcdn.com/images/longlivedrgn/post/1a9775ee-e400-45ad-9503-f11913c1805f/image.png

위의 두 아이폰 기종을 통하여 priority에대해 이해를 해보자.

  • 먼저 아래의 코드는 button을 view에 추가하고 leading, trailing만 safe area에 맞춰준 상태이다.
// 1)
let safeBottonAnchor = button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
safeBottonAnchor.isActive = true
safeBottonAnchor.priority = .init(999)

// 2)
// priority를 설정하지 않으면 자동으로 .required이다.
let viewBottonAnchor = button.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: -20)
viewBottonAnchor.isActive = true
  • 아이폰 14의 경우, 위의 코드를 돌려보면 아래와 같이 view가 그려진다.

https://velog.velcdn.com/images/longlivedrgn/post/b650a3b6-5ef6-45d3-94ae-4b345323d5a1/image.png

→ 즉, 먼저 priority가 높은 2)의 constraint를 따라가지만, 1)의 제한 조건이 2)의 조건도 만족하므로, 최종적으로는 1)의 constraint를 따라가게된다.

  • 아이폰 SE의 경우, 위의 코드를 돌려보면 아래와 같이 view가 그려진다.

https://velog.velcdn.com/images/longlivedrgn/post/76ecd302-9b45-4229-b0e8-85463c9fa32a/image.png

→ 즉, SE 또한 priority가 높은 2)의 constraint를 따라가고, 1)의 제한 조건이 2)의 조건을 만족하지 않으므로, 최종적으로는 2)의 constraint를 따라가게된다.

위와 같이 priority를 활용하면, 14나 SE 둘 다에서도 적당한 bottom inset을 가지게 설정할 수 있다.(super view의 bottom에서 적당한 위치에 떠 있는..)

저작자표시 (새창열림)

'iOS' 카테고리의 다른 글

[iOS] Delegate Pattern  (0) 2023.02.04
[iOS] Queue 구현하기  (0) 2023.02.04
[iOS] prepareForReuse()  (0) 2023.02.03
[iOS] TableView의 reloadData()란?  (0) 2023.02.03
[iOS] iOS 프로젝트 파일 폴더링  (0) 2023.02.03
    'iOS' 카테고리의 다른 글
    • [iOS] Delegate Pattern
    • [iOS] Queue 구현하기
    • [iOS] prepareForReuse()
    • [iOS] TableView의 reloadData()란?
    longlivedrgn
    longlivedrgn

    티스토리툴바