오늘은 contentMode에 대해서 알아보려고 한다. iOS 개발한 경험이 있으면 누구나 사용한 적이 있을 것이다. 나 또한 아무 생각없이 사용하다가 많이 사용하는 기능 중 하나라고 생각해서 이쯤에 정리해보고자 한다.
애플 공식 문서부터 읽어보쟈!!!
A flag used to determine how a view lays out its content when its bounds change.
뷰의 경계가 바뀔 때 해당 뷰의 컨텐츠를 배치하는 방식을 결정하는 flag
var contentMode: UIView.ContentMode { get set }
contentMode의 옵션은 많지만 가장 많이 사용하는 ScaleToFill, AspectFit, AspectFill에 대해서만 정리해보고자 한다!
The option to scale the content to fit the size of itself by changing the aspect ratio of the content if necessary.
원본 이미지의 비율을 무시하고 전체 이미지가 다 나올 수 있도록 이미지를 꽉 채우는 옵션
The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
원본 이미지의 비율을 유지한 채 컨텐츠를 채우는 방식으로, 남은 공간은 비어진다.
The option to scale the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.
원본 이미지의 비율을 맞추면서 해당 뷰의 컨텐츠를 꽉 채우는 방식으로, 이미지의 부분이 잘릴 수도 있다.
AspectFit, AspectFill를 비교해보자면 원본의 이미지 비율을 유지하는 것은 동일하지만 AspectFit은 비율을 유지한채 나머지 뷰의 컨텐츠는 비워지지만 AspectFill는 원본이미지를 꽉 채운채 이미지의 비율을 유지하기 때문에 원본 이미지가 짤린다는 것!!!!
이제는 원하는 용도에 맞게 contentMode를 더 잘 설정할 수 있겠다!!
끄읕~