Created
June 18, 2021 16:10
-
-
Save froggomad/501579a142644422d3da56e6dd11ec42 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 { | |
var produce: Produce? { | |
didSet { | |
titleLabel.text = produce?.description | |
} | |
} | |
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(produce: Produce) { | |
super.init(frame: .zero) | |
// didSet not available in init | |
defer { self.produce = produce } | |
self.translatesAutoresizingMaskIntoConstraints = false | |
// allow gestures to propagate through views | |
isUserInteractionEnabled = true | |
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