0

I'm implementing some UICollectionViewCell classes to populate a normal UICollectionView embedded in a NavigationController.

In this cell class, I need to get the NavigationBar height of the collectionView's navigationController that's going to present this cell.

My issue is that I need this data upfront, on the cells creation process and not afterwards.

If it was after I'd simply cast the cell inside the cellForItemAt and fill some property there , but I need this upfront.

What's the best way to approach this?

This would be the same as a regular tableViewCell How would you access the NavigationController.navigationBar height inside the cell class?

I know that mostly the height is 44 but I don't want to force this value, because it may change from devices, and if I access that property the value will always be correct and errorless.

This isn't the regular call of the navigationController.navigationBar.frame.height because inside the cell class there isn't a navigationController

Thanks for helping.

Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73
  • Possible duplicate of [Programmatically get height of navigation bar](https://stackoverflow.com/questions/7312059/programmatically-get-height-of-navigation-bar) – koen May 23 '17 at 19:23
  • this shouldn't be set as duplicated because the context is different – Ivan Cantarino May 23 '17 at 19:28
  • Unless Apple changes something in future iOS versions, the nav bar height is `44` always, regardless of device. Now, I understand your desire to have your code clean and access the value programatically, but it seems like an overkill for this. Also what do you mean by `cells creation process`? There are many steps to it, which exactly do you mean? – Losiowaty May 23 '17 at 19:33
  • Well basically I've my regular UICollectionViewCell class right? Then inside it I've some properties and objects which some of them I'm anchoring programmatically and since in this specific cell I'm creating an header cell, I need to get the proper sizes so it all animates properly afterwards in the UICollectionView controller itself. Great you understood my POV ;) – Ivan Cantarino May 23 '17 at 19:35
  • If you just want to create a header in your `UICollectionView`, maybe this will help you then. http://www.appcoda.com/supplementary-view-uicollectionview-flow-layout/ – koen May 23 '17 at 20:01

1 Answers1

0

Use this code inside your cell to get the UIViewController:

extension UIView
{
    func viewController() -> UIViewController? {
        var responder : UIResponder? = self
        while let r = responder {
            if r.isKind(of: UIView.self) {
                responder = r.next
            } else {
                break
            }
        }
        return responder as? UIViewController
    }
}

Then, you can call viewController.navigationController.navigationBar.frame.height like you intended.

TawaNicolas
  • 636
  • 1
  • 5
  • 11
  • hmm... nice tip; let me try it out in swift :D – Ivan Cantarino May 23 '17 at 19:30
  • Will this work if the cell is not yet present, and not dequeued? From the question we can infer that OP wants this information before `cellForItem` gets called. – Losiowaty May 23 '17 at 19:35
  • indeed. I need the info on-creation, when I'm anchoring the cells objects inside it, and not in the cellForItemAt, anyway I'm trying to access if with this example, if it doesn't work, I'll just hard-code it to 44px – Ivan Cantarino May 23 '17 at 19:39
  • @IvanCantarino Oops sorry I just noticed that this is for Swift, I updated the code for you. – TawaNicolas May 23 '17 at 19:43
  • @Losiowaty This won't work before the cell is dequeued. You can't call it from awakeFromNib(). – TawaNicolas May 23 '17 at 19:44
  • that's what I just tested. it indeed doesn't work before I dequeue the cell itself but it was a great info anyway, I usually don't use responders in this way. thanks anyway :D – Ivan Cantarino May 23 '17 at 19:48