Search code examples
iosobjective-cuiviewcontrollernslayoutconstraintchildviewcontroller

Adding a second child view controller using NSLayoutContraints, always position it under the Navigation bar, not after the Navigationbar


am facing a strange problem with the UI. Please find below details.

  • I created a sample demo project to showcase the issue. The requirement is pretty simple (Attached screen shots too). The app starts with a ViewController which is a UINavigationController, so it shows a NavigationBar.
  • A UISegmentController of 2 elements has added to the UINavigationBar.
  • There are 2 child TableViewControllers which will be shown for each segment selection.
  • Each ViewController is added using NSLayoutConstraints. And every time, older child VC will be removed and new one will be added instead of hiding and showing. Though we don't create the VC object every time. It has a strong reference
  • Child VCs are added to RootVC's view instead of having a ContainerView. Tried ContainerView also but same result.

Now the problem is, the second child VC is always placed under the Navigation bar(Please refer the screen shot attached). But the first one is perfectly fine as its laid below the NavigationBar. if I set the NavigationBar translucent property to "NO", both works fine as expected. But with NavigationBar translucent property as "YES", only the first one will be laid properly not the second one. It doesn't matter which ChildViewController(firstVC or secondVC), it happens for the childVC added after the first one. Any insights to this strange issue? I've created a sample demo project if you guys want to check whats going on there..

Thanks in advance..

First VC Second VC


Solution

  • Did you disable the option : Auto adjust scrolls inset ? it is buggy and apply only to the first Scrollview of you view controller