본문 바로가기

iOS

UIView - bounds and frame 2탄

우와! 2탄!

퇴근 후 간단하게 작성하는 글이니 편안한 문체 이해해주시면 감사하겠습니다.

 

UIView - bounds

 

UIView - frame

UIView의 프로퍼티 중 하나인 bounds와 frame은 뷰의 위치크기를 나타내는 속성이다.

CGRect 타입이며 origin과 size로 이루어져 있다. origin은 위치를 나타내며 size는 말 그대로 크기를 나타낸다. size는 bounds와 frame 모두 동일하다. 그런데 재밌는 점은 frame의 공식문서 설명을 보면 

// frame
Setting this property changes the point specified by the center property and changes the size in the bounds rectangle accordingly. 

// bounds
Changing the size also changes the size of the rectangle in the frame property to match.

frame의 size는 bounds의 size가 변경될 때 변경된다고 적혀있다.

그렇다면 bounds는 언제 결정되는걸까?

 

layoutSubviews

the default implementation uses any constraints you have set to determine the size and position of any subviews.

layoutSubviews() 메서드가 호출될 때 정해진다. 왜 이 때 정해질까?! 에 대한 의문은 나중에 view drawing cycle에 대해 알아볼 때 자세히 이야기해보겠다.

 

bounds의 재밌는점 2.

Changing the bounds rectangle automatically redisplays the view without calling its draw(_:) method. If you want UIKit to call the draw(_:) method, set the contentMode property to UIView.ContentMode.redraw.

bounds는 뷰의 위치와 크기를 나타내는데 bounds가 바뀐다고 해서 draw 메소드가 호출되진 않는다.

bounds가 변경될 때 draw가 호출되길 원한다면 UIView.contentMode를 .redraw로 설정하면 된다.