View extends under NavigationBar when UISearchController is active, although edgesForExtendedLayout = []

In my ViewController, which is shown within a UINavigationController, I need the very old (pre iOS6) behaviour where the view does not extend under the navigation bar. For that, I set edgesForExtendedLayout = [], which works fine so far. I also have a UISearchController set in the navigationItem.


The problem occurs in the following situation: The SearchController is active (i.e. text field has the focus), and another view controller is pushed. When I tap "back", the view of my view controller now suddenly extends under the navigation bar.


Here is a minimal example:


class TestViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let searchViewController = UISearchController(searchResultsController: nil)
        searchViewController.obscuresBackgroundDuringPresentation = false
       
        view.backgroundColor = .red
        edgesForExtendedLayout = []
        navigationItem.searchController = searchViewController
        definesPresentationContext = true
       
        navigationController?.view.backgroundColor = .green
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
       
        let button = UIButton(type: .system)
        button.setTitle("Push", for: .normal)
        button.frame = .init(x: 10, y: 100, width: 100, height: 40)
        button.addTarget(self, action: #selector(self.pushViewController), for: .touchUpInside)
        view.addSubview(button)
    }
   
    @objc func pushViewController() {
        let nextViewController = UIViewController()
        navigationController?.pushViewController(nextViewController, animated: true)
    }
}


class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let vc = TestViewController()
        let nc = UINavigationController(rootViewController: vc)
        window.rootViewController = nc
        window.makeKeyAndVisible()
        return true
    }
}

Any ideas?

I have exact the same problem. Could you find a solution in the meantime?


I know that edgesForExtendedLayout is to be replaced by UILayoutGuide, but so far I was unable to get it working.

Just tested with Xcode 12 Beta and my issue is gone.
View extends under NavigationBar when UISearchController is active, although edgesForExtendedLayout = []
 
 
Q