Skip to content

Instantly share code, notes, and snippets.

@dkulundzic
Last active February 2, 2023 12:47
Show Gist options
  • Save dkulundzic/c691a07c6a0ae2ac2eb7a8228e316511 to your computer and use it in GitHub Desktop.
Save dkulundzic/c691a07c6a0ae2ac2eb7a8228e316511 to your computer and use it in GitHub Desktop.
A UIKit UIView with programmatic Auto Layout based layout
class ActivitiesListContentView: UIView {
var flowLayout: ActivitiesListFlowLayout? {
collectionView.collectionViewLayout as? ActivitiesListFlowLayout
}
private(set) lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: ActivitiesListFlowLayout())
private(set) var categorySelectorView: UIView?
private lazy var categorySelectorContainerView = UIView.autolayoutView()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
flowLayout?.setEstimatedSize(constrainedToWidth: collectionView.bounds.width)
}
}
extension ActivitiesListContentView {
func embedCategorySelector(_ view: UIView) {
guard !subviews.contains(view) else {
return
}
categorySelectorContainerView.addSubview(view)
view.snp.makeConstraints {
$0.edges.equalToSuperview()
}
categorySelectorView = view
}
}
// MARK: - Private Methods
private extension ActivitiesListContentView {
func setupViews() {
setupCategorySelectorContainerView()
setupCollectionView()
}
func setupView() {
backgroundColor = .white
}
func setupCategorySelectorContainerView() {
addSubview(categorySelectorContainerView)
categorySelectorContainerView.snp.makeConstraints {
$0.leading.trailing.equalToSuperview()
$0.top.equalToSuperview().inset(16)
}
}
func setupCollectionView() {
addSubview(collectionView)
collectionView.snp.makeConstraints {
$0.leading.trailing.bottom.equalToSuperview()
$0.top.equalTo(categorySelectorContainerView.snp.bottom).offset(16)
}
collectionView.backgroundColor = .clear
collectionView.register(ActivitiesListCell.self)
collectionView.setContentHuggingPriority(.defaultLow, for: .vertical)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment