Skip to content

Commit

Permalink
Update CardsViewController.swift (intuit#116)
Browse files Browse the repository at this point in the history
* Update CardsViewController.swift

Fixing issues with fixed width on CardCell content not matching CollectionView width on screen resizing

* Update CardsViewController.swift

removing unecessary call to setupConstraints, called on binding anyways
  • Loading branch information
sebastienba authored and croossin committed Feb 15, 2019
1 parent 95c3fae commit 1ee83a0
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions CardParts/src/Classes/CardsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ struct CardInfo: Equatable {
}

open class CardsViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate {


let cardCellWidth = Variable(CGFloat(0))
let editButtonOffset : CGFloat = 24
let editButtonHeight : CGFloat = 50
let editButtonWidth : CGFloat = 50
Expand Down Expand Up @@ -81,7 +82,12 @@ open class CardsViewController : UIViewController, UICollectionViewDataSource, U

view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[collectionView]|", options: [], metrics: nil, views: ["collectionView" : collectionView]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[collectionView]|", options: [], metrics: nil, views: ["collectionView" : collectionView]))
cardCellWidth.value = view.bounds.width - (CardParts.theme.cardCellMargins.left + CardParts.theme.cardCellMargins.right)
}

open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
cardCellWidth.value = size.width - (CardParts.theme.cardCellMargins.left + CardParts.theme.cardCellMargins.right)
invalidateLayout()
}

// functionality that happens when the view appears
Expand All @@ -96,7 +102,7 @@ open class CardsViewController : UIViewController, UICollectionViewDataSource, U
public func invalidateLayout() {
DispatchQueue.main.async { [weak self] in
let context = UICollectionViewFlowLayoutInvalidationContext()
context.invalidateFlowLayoutAttributes = false
context.invalidateFlowLayoutAttributes = true
self?.layout.invalidateLayout(with: context)
}
}
Expand Down Expand Up @@ -205,14 +211,11 @@ open class CardsViewController : UIViewController, UICollectionViewDataSource, U
cell.cardContentView.addSubview(viewController.view)
viewController.view.translatesAutoresizingMaskIntoConstraints = false

cell.cardContentView.removeConstraints(cell.cardContentConstraints)
cell.cardContentConstraints.removeAll()

let metrics = ["cardContentWidth": view.bounds.size.width - (CardParts.theme.cardCellMargins.left + CardParts.theme.cardCellMargins.right)]
cell.cardContentConstraints.append(contentsOf:NSLayoutConstraint.constraints(withVisualFormat: "H:|[view(cardContentWidth)]|", options: [], metrics: metrics, views: ["view" : viewController.view]))
cell.cardContentConstraints.append(contentsOf:NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: [], metrics: nil, views: ["view" : viewController.view]))

cell.cardContentView.addConstraints(cell.cardContentConstraints)
cardCellWidth.asObservable().bind { [weak self, weak cell, weak viewController] (value) in
if let strongSelf = self , let cell = cell, let viewController = viewController {
strongSelf.setupConstraints(cell, viewController: viewController)
}
}.disposed(by: bag)
if getEditModeForIndexPath(indexPath: indexPath) {
let editButton = UIButton(frame: CGRect(x: view.bounds.size.width - editButtonOffset - editButtonWidth, y: 0, width: editButtonWidth, height: editButtonHeight))
editButton.setImage(UIImage(named: editButtonImage, in: Bundle(for: CardsViewController.self), compatibleWith: nil), for: .normal)
Expand All @@ -230,6 +233,21 @@ open class CardsViewController : UIViewController, UICollectionViewDataSource, U
cell.cardContentView.layoutIfNeeded()
cell.cardContentView.updateConstraints()
return cell
}

func setupConstraints(_ cell: CardCell, viewController: UIViewController) {

cell.cardContentView.removeConstraints(cell.cardContentConstraints)
cell.cardContentConstraints.removeAll()

let metrics = ["cardContentWidth": cardCellWidth.value]

cell.cardContentConstraints.append(contentsOf:NSLayoutConstraint.constraints(withVisualFormat: "H:|[view(cardContentWidth)]|", options: [], metrics: metrics, views: ["view" : viewController.view]))
cell.cardContentConstraints.append(contentsOf:NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: [], metrics: nil, views: ["view" : viewController.view]))


cell.cardContentView.addConstraints(cell.cardContentConstraints)

}

open func getCardControllerCount() -> Int {
Expand Down

0 comments on commit 1ee83a0

Please sign in to comment.