Search code examples
iosanimationuinavigationcontrolleruinavigationbaruipageviewcontroller

Weird animation when hiding the navigation bar


I'm implementing a photo gallery via a UIPageViewController subclass (called GalleryController) embedded in a UINavigationController. The navigation bar hides when I tap, but the animation is odd:

https://www.youtube.com/watch?v=9SLF3Nq3uNE

Here's the code in GalleryController:

  override var navigationItem: UINavigationItem {
    let item = super.navigationItem
    // Access super and add items to it.
    // Don't create a new UINavigationItem instance — that breaks the back button.

    let space = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    space.width = touchSize / 2

    item.rightBarButtonItems = [
      UIBarButtonItem(title: "All Photos", style: .plain, target: self, action: #selector(showAllPhotos)),
      space,
      UIBarButtonItem(title: "Delete", style: .plain, target: self, action: #selector(deletePhoto))
    ]
    return item
  }

This problem started occuring only after the page view controller was introduced.

What's causing this, and how do I fix it?


Solution

  • The solution turned out to be to do the following in the init of the UIPageViewController subclass:

    automaticallyAdjustsScrollViewInsets = false
    

    I don't know why it works, but it does.

    I was earlier doing this in the child view controller (that represents a single page of the page view controller), but that wasn't working.