Here is my add code for this bar.

var searchBar = UISearchBar() searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true navigationItem.titleView = searchBar 

searchBar.heightAnchor.constraint (equalToConstant: 44) .isActive = true to prevent the bar from extending the navigationBar

On iOS 11.1, it looks like this enter image description here On iOS 11.3, it looks fine enter image description here

The question is how to get back to normal on iOS 11.1

    1 answer 1

    The problem with the back button, because she does not customize the width herself.
    We need to find this button and set its width:

     func searchAndFixBackButton(in view: UIView) { if ["_UIBackButtonContainerView", "_UIButtonBarButton"].contains(String(describing: type(of: view))) { view.widthAnchor.constraint(equalToConstant: 44).isActive = true } for subview in view.subviews { searchAndFixBackButton(in: subview) } } // MARK: - ΠΏΡ€ΠΈΠΌΠ΅Π½ΡΡ‚ΡŒ Π² viewDidAppear: override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) searchAndFixBackButton(in: navigationController!.view) searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true navigationItem.titleView = searchBar } //Π»ΠΈΠ±ΠΎ добавляСтС Π² viewDidLoad Π’Π°Ρˆ searchBar ΠΈ ΠΏΡ€Π°Π²ΠΈΡ‚Π΅ Ρ€Π°Π·ΠΌΠ΅Ρ€: override func updateViewConstraints() { searchAndFixBackButton(in: navigationController!.view) super.updateViewConstraints() } //Π»ΠΈΠ±ΠΎ добавляСтС Π² viewDidLoad ΠΈ Π² viewDidLayoutSubviews: override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() searchAndFixBackButton(in: navigationController!.view) searchBar.sizeToFit() } //Π’Π°ΠΊΠΆΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΡƒ Π½Π° Π²Π΅Ρ€ΡΠΈΡŽ, Ссли эта ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π² 11.1: if UIDevice.current.systemVersion == "11.1" { //Ѐиксим } 
    • The answer is correct, but since it is done in viewDidApear, first empty NavigationBar, and then the HOBA, the search line flies - Daniyal1909
    • @ Daniyal1909 updated the answer. - VAndrJ