본문 바로가기
swift

Swift: Frame과 Bounds의 차이

by 유순이 2021. 6. 6.

모든 UIView subclass는 얼핏 보면 비슷하게 보이는 두 가지 특징을 갖습니다 : frame과 bounds. 둘 모두 (너비, 높이 그리고 X, Y축 위치를 가진) CGRect을 리턴한다고는 하지만, 이것은 이 둘이 똑같다고 말하는 것은 아닙니다.

 

가장 쉽게 이야기하자면, 어떤 뷰의 bounds는 본인의 스페이스와 연관된 좌표값을 참조합니다( 마치 해당 뷰가 독립적인 것 처럼 ). 반면에 frame은 그 부모 뷰의 스페이스와 연관된 좌표값을 참조합니다.

 

이것은 다음과 같은 의미를 갖습니다:

  • 만약 당신이 좌표(0, 0)에 너비, 높이가 100인 frame과 bounds를 만든다면, 이 둘은 똑같다고 할 수 있습니다.
  • 만약 해당 뷰를 x축으로 100 만큼 이동한다면, frame은 해당 변화를 반영하겠지만, bounds는 변화가 있지 않습니다. bounds는 스스로의 공간과 연관이 되어 있기 때문에, 뷰 자체가 변하지 않은 해당 변화는 bounds에 영향을 주지 않습니다.
  • 만약 당신이 뷰를 회전시킨다거나, 규모를 키우는 등 뷰를 변형시킨다면, frame은 해당 변화를 반영하지만, bounds는 여전히 변화가 없을 것입니다 ( 그 뷰가 내부적인 영향이 없는 한, 바뀌지 않을 것입니다.)

 

당신이 frame과 bounds의 너비와 높이를 바꾸었을 때, 다른 value들도 update될 것입니다. 일반적으로 당신이 bounds를 변경하고, UIKit가 알아서 frame을 계산해주는 것을 추천합니다.


All UIView subclasses have two properties that at first glance seem similar: frame and bounds. Both return a CGRect – a rectangle containing their X and Y position, plus their width and height – but that doesn’t mean they are the same.

At its simplest, a view’s bounds refers to its coordinates relative to its own space (as if the rest of your view hierarchy didn’t exist), whereas its frame refers to its coordinates relative to its parent’s space.

This means a few things:

  1. If you create a view at X:0, Y:0, width:100, height:100, its frame and bounds are the same.
  2. If you move that view to X:100, its frame will reflect that change but its bounds will not. Remember, the bounds is relative to the view’s own space, and internally to the view nothing has changed.
  3. If you transform the view, e.g. rotating it or scaling it up, the frame will change to reflect that, but the bounds still won’t – as far as the view is concerned internally, it hasn’t changed.

When you change the width or height of either frame or bounds, the other value is updated to match. Generally it’s better to modify bounds plus center and transform, and let UIKit calculate the frame for you.

 

referece: https://www.hackingwithswift.com/example-code/uikit/whats-the-difference-between-frame-and-bounds

 

What’s the difference between frame and bounds? - free Swift 5.4 example code and tips

Was this page useful? Let us know! 1 2 3 4 5

www.hackingwithswift.com

 

 

'swift' 카테고리의 다른 글

Optional  (0) 2021.07.05
Func 고급기능  (0) 2021.07.05
iOS 앱 개발 : Casting  (0) 2021.05.17
iOS 앱 개발 : var / let  (0) 2021.05.17
iOS 앱 개발 : ViewController  (0) 2021.05.11

댓글