Skip to content

Instantly share code, notes, and snippets.

@kutan74
Last active October 27, 2019 06:18
Show Gist options
  • Save kutan74/7b965c43219f6373e26892abcab5e87d to your computer and use it in GitHub Desktop.
Save kutan74/7b965c43219f6373e26892abcab5e87d to your computer and use it in GitHub Desktop.
import UIKit
class TWReusableCollectionViewCell: UICollectionViewCell {
var slot: UIView! {
didSet {
layoutViews()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init (coder:) has not been implemented")
}
lazy var width: NSLayoutConstraint = {
let width = contentView.widthAnchor.constraint(equalToConstant: bounds.size.width)
width.isActive = true
return width
}()
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
width.constant = bounds.size.width
return contentView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: 1))
}
}
// MARK: Constraints
extension TWReusableCollectionViewCell {
func layoutViews() {
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(slot)
slot.anchor(top: contentView.topAnchor, leading: contentView.leadingAnchor,
bottom: nil, traling: contentView.trailingAnchor)
/// Making sure the bottom constraint of ContentView is equal to bottom constarint of the parent view
contentView.bottomAnchor.constraint(equalTo: slot.bottomAnchor).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment