What method is called when UINavigationController is presented?

237 Views Asked by At

I am trying to subclass the UINavigationController to add a custom subview underneath its UINavigationBar. The problem is when I set my custom UINavigationController as the rootViewController of my window, the UINavigationBar position changes, so I want to update the position of my custom view accordingly. My question is where should I put my code to update my custom view.

    var navController = TabbedNavigationController(rootViewController: firstView)

    // navController.navigationBar.frame.origin.y = 0
    self.window?.rootViewController = navController
    self.window?.makeKeyAndVisible()
    // navController.navigationBar.frame.origin.y = 20.0
2

There are 2 best solutions below

5
On BEST ANSWER

What you are trying to do is respond to the positioning of subviews of the navigation controller's view. Positioning of subviews is called layout, and takes place in layoutSubviews. Thus, you could try implementing viewDidLayoutSubviews, which is called immediately after layoutSubviews. Note that it can be called many times but you may want to add your subview only once, so be careful to add appropriate guards.

0
On

You can force the "view" of the ViewController to extend underneath the UINavigationBar. Here's some code to help you out, it's not Swift, it's ObjC but you will get the point. This is from my custom implementation of UIVeiwController subclass. I also have a full implementation of UINavigationController and UINavigationBar to adjust the size in points of the UINavigationBar. I can tell you that you can do what Apple has done in their example by just following the code I'm linking here.

[[[self navigationController] navigationBar] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[[self navigationController] navigationBar] setShadowImage:[UIImage new]];
[[[self navigationController] navigationBar] setTranslucent:TRUE];
[[[[self navigationController] navigationBar] layer] setShadowOpacity:0.0f];
[[[[self navigationController] navigationBar] layer] setShadowOffset:CGSizeMake(0.0f, 0.0f)];