Skip to content

Instantly share code, notes, and snippets.

@cozzin
Last active August 9, 2018 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cozzin/fea82d3574809a87f1f949c17f7fd543 to your computer and use it in GitHub Desktop.
Save cozzin/fea82d3574809a87f1f949c17f7fd543 to your computer and use it in GitHub Desktop.
여러 UI 컴포넌트들이 있는 HeaderView의 높이 계산해주기
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let headerView = headerView(at: section) else {
return nil
}
return headerView.systemLayoutSizeFitting(CGSize(width: targetView.frame.width, height: CGFloat.greatestFiniteMagnitude), withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel).height
}
@cozzin
Copy link
Author

cozzin commented Aug 9, 2018

UILabel 하나만 HeaderFooterView의 subview로 추가된다면 상관없지만
여러개의 UILabel 을 HeaderFooterView로 사용하는 경우 문제가 발생 했었다.

func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize

systemLayoutSizeFitting 만 쓰게되면 높이를 계산할 때 priority를 제대로 결정하지 못하는 이슈가 발생한다.
그래서 헤더에 여러 UI 요소들이 있을 때는 priority를 설정해 줄 수 있는 아래와 같은 함수를 사용하도록 하자.

func systemLayoutSizeFitting(_ targetSize: CGSize, 
withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, 
     verticalFittingPriority: UILayoutPriority) -> CGSize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment