Last active
June 18, 2021 15:04
-
-
Save froggomad/042c1b4208aa4ba5a1809b6a84c33ac9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class TitledCollectionView: UIView { | |
private lazy var vStack: UIStackView = { | |
let stack = UIStackView(arrangedSubviews: [titleLabel, collectionView]) | |
stack.translatesAutoresizingMaskIntoConstraints = false | |
stack.axis = .vertical | |
stack.distribution = .fill | |
stack.alignment = .fill | |
stack.spacing = 10 | |
return stack | |
}() | |
private lazy var titleLabel: UILabel = { | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.font = .preferredFont(forTextStyle: .title1) | |
label.numberOfLines = 1 | |
return label | |
}() | |
private lazy var collectionView: UICollectionView = { | |
let collectionView = UICollectionView(frame: .zero) | |
collectionView.translatesAutoresizingMaskIntoConstraints = false | |
return collectionView | |
}() | |
/// programmatic init | |
init(title: String) { | |
super.init(frame: .zero) | |
self.translatesAutoresizingMaskIntoConstraints = false | |
// allow gestures to propagate through views | |
isUserInteractionEnabled = true | |
self.titleLabel.text = title | |
setupViews() | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
setupViews() | |
} | |
private func setupViews() { | |
backgroundColor = .systemBackground | |
addSubview(vStack) | |
NSLayoutConstraint.activate([ | |
vStack.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor), | |
vStack.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor), | |
vStack.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor), | |
vStack.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor) | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment