Skip to content

Instantly share code, notes, and snippets.

@spraveenk91
Created October 11, 2021 18:52
Show Gist options
  • Save spraveenk91/e0139551f4a2ae1d268829c10df86315 to your computer and use it in GitHub Desktop.
Save spraveenk91/e0139551f4a2ae1d268829c10df86315 to your computer and use it in GitHub Desktop.
class CardView: UIView {
@IBOutlet weak var title: UILabel!
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var collectionView: UICollectionView!
func setupCardView() {
.....
}
override func layoutSubviews() {
super.layoutSubviews()
if !__CGSizeEqualToSize(collectionView.bounds.size, collectionView.intrinsicContentSize) {
collectionView.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
return CGSize(width: self.frame.width, height: self.frame.height)
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
//NotificationCenter.default.addObserver(self, selector: #selector(handleTap), name: NSNotification.Name("HandleTap"), object: nil)
}
@objc func handleTap() {
resizeToFitSubviews()
}
/// Method to resize the View
func resizeToFitSubviews() {
var width: CGFloat = 0
var height: CGFloat = 0
for someView in self.subviews {
let aView = someView
let newWidth = aView.frame.origin.x + aView.frame.width
let newHeight = aView.frame.origin.y + aView.frame.height
width = max(width, newWidth)
height = max(height, newHeight)
}
frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: width, height: height)
self.layoutIfNeeded()
}
}
extension CardView {
/// private function to set viewmodel's bindings
private func setupBindings() {
......
}
/// private function to set datasource for collectionview
private func setupCardViewDataSource() {
cardDataSource = RxCollectionViewSectionedReloadDataSource
<SectionModel<String, CardModel>> {(_, _, indexPath, element) -> UICollectionViewCell in
return self.configureCell(cardModel: element, indexPath: indexPath)
}
if let source = cardDataSource {
viewModel.cardItems.bind(to: collectionView.rx.items(dataSource: source)).disposed(by: disposeBag)
}
}
}
extension CardView: UICollectionViewDelegate {
/// private function to configure CardView
private func configureCardView() {
self.collectionView.register(
UINib(nibName: "CardCell",
bundle: nil),
forCellWithReuseIdentifier: "CardCell")
collectionView.rx.setDelegate(self).disposed(by: disposeBag)
setupCardViewDataSource()
collectionView.setContentOffset(CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude), animated: false)
let snappingLayout = Layout()
snappingLayout.snapPosition = .center
snappingLayout.scrollDirection = .horizontal
collectionView.collectionViewLayout = snappingLayout
collectionView.decelerationRate = .fast
collectionView.layoutIfNeeded()
}
func configureCell(cardModel: CardModel, indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: "CardCell",
for: indexPath) as? CardCollectionViewCell {
cell.cardModel = cardModel
cell.configureCell()
return cell
}
return UICollectionViewCell()
}
}
extension CardView: UICollectionViewDelegateFlowLayout {
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.frame.width, height: self.frame.height)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment