Search code examples
iosswiftuinavigationcontrolleruinavigationbar

Wrong position of rightBarButtonItem in iOS 10


I want to put the view of a UINavigationController into another view controller's view. The problem is that the right bar button is misplaced on iOS 10. On iOS 8 and 9 it looks good.

iOS 8 and 9:

enter image description here

iOS 10:

enter image description here

Here's the code how i add the navigation controller:

override func viewDidLoad() {
    super.viewDidLoad()

    let viewController = MyViewController()
    navController = UINavigationController(rootViewController: viewController)
    navController.willMove(toParentViewController: self)
    addChildViewController(navController)
    navController.view.frame = view.bounds
    view.addSubview(navController.view)
    navController.didMove(toParentViewController: self)
}

In MyViewController

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem(
        barButtonSystemItem: .done,
        target: self,
        action: #selector(doneButtonPressed)
    )
}

Also there are no clipped views (except the button itself). See snapshot of IB Debug View Hierachy:

enter image description here

Any ideas?


Solution

  • I solved the problem by forwarding the viewWillAppear and viewDidAppear events to the navigation controller's root view controller.

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        navController.viewControllers.first?.viewWillAppear(animated)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        navController.viewControllers.first?.viewDidAppear(animated)
    }