I have a controller that has multiple collectionView on it and I want to scroll as one. So I added an .addObserver to the collectionViews but I had issues with differentiating the two collectionViews so I added a context.
My question is if this is the correct way to differentiating the collectionViews for the "override func observeValue"?
Everything looks like its working fine but don't know if this is the correct way to do this and if I will have future issues?
private static var InicioRecetasCollectionViewContext = UnsafeMutableRawPointer.allocate(byteCount: 4 * 4, alignment: 1)
private static var InicioPedidoServerCollectionViewContext = UnsafeMutableRawPointer.allocate(byteCount: 4 * 4, alignment: 1)
//Set the heigh of the table view so it scrolls as one.
override func viewWillAppear(_ animated: Bool) {
self.InicioRecetasCollectionView.addObserver(self, forKeyPath: "contentSize", options: .new, context: InicioViewController.InicioRecetasCollectionViewContext)
self.InicioPedidoServerCollectionView.addObserver(self, forKeyPath: "contentSize", options: .new, context: InicioViewController.InicioPedidoServerCollectionViewContext)
}
override func viewWillDisappear(_ animated: Bool) {
self.InicioRecetasCollectionView.removeObserver(self, forKeyPath: "contentSize")
self.InicioPedidoServerCollectionView.removeObserver(self, forKeyPath: "contentSize")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print("contentSize")
if keyPath == "contentSize" {
if let newValue = change?[.newKey]{
let newSize = newValue as! CGSize
if(context == InicioViewController.InicioRecetasCollectionViewContext){
self.InicioProveedorAvisosCollectionViewHeightConstraint.constant = newSize.height
}
if(context == InicioViewController.InicioPedidoServerCollectionViewContext){
self.InicioPedidoServerCollectionViewHeightConstraint.constant = newSize.height
}
}
}
}