ios – How you can stop a backBarButton’s title disappear when add a Inline Search Bar to Navigation Bar in SwiftUI

Spread the love


We’ve an current undertaking which is construct in UIKit, and now I wish to have a brand new view created in SwiftUI with Search Bar keep on the navigation bar.

Which search bar is INLINE with the backBarButtonItem, so clearly modifier searchable can not fulfill my requirement.

The simplify stream may be simplify like this
enter image description here


However when I attempt to do a proof of idea. The code will likely be on the final part of this query.

I came upon that after pushing out the title of backBarButtonItem is proven on the first second then disappear when the search bar added. Which is absolutely dangerous in expertise.

enter image description here

My Query

  1. Is there any method to repair?
  2. and why it’s habits like that?


Thanks for Sweeper ‘s trace, once I tried to alter the barButtonTitle to a shorter string, the textual content will keep there:

// In ViewController's viewDidLoad
navigationItem.backButtonTitle = "A"

shorter backButtonTitle

Subsequently, the brand new query turning into like

  • How you can increased backBarButtonItem ‘s compress resistance
  • How you can cease the backButtonTitle from hidden

I attempted to look with my new query however did not get any helpful answer. If it is onerous to do it both with SwiftUI and UIKit, I can contemplating and persuade our designer to not present the backButtonTitle.


ViewController in UIKit

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {

        view.backgroundColor = .white
        title = "Demo"

        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, goal: self, motion: #selector(didSelectCancel(sender:)))

    @objc func didSelectCancel(sender: UIBarButtonItem) {
        let hostingController = UIHostingController(rootView: MyView())
        self.navigationController?.pushViewController(hostingController, animated: true)

MyView in SwiftUI

import SwiftUI

struct MyView: View {
    var physique: some View {
        ScrollView {
            Textual content("Hiya")
        .toolbar {
            ToolbarItem(placement: .principal) {
                SearchBar(textual content: .fixed("check"), placeholder: "check")


Since SwiftUI would not present stand alone SearchBar, so I seize one from article Making a search bar for SwiftUI and utilizing it in my PoC

struct SearchBar: UIViewRepresentable {

    @Binding var textual content: String
    var placeholder: String

    class Coordinator: NSObject, UISearchBarDelegate {

        @Binding var textual content: String

        init(textual content: Binding<String>) {
            _text = textual content

        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            textual content = searchText

    func makeCoordinator() -> SearchBar.Coordinator {
        return Coordinator(textual content: $textual content)

    func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
        let searchBar = UISearchBar(body: .zero)
        searchBar.delegate = context.coordinator
        searchBar.placeholder = placeholder
        searchBar.searchBarStyle = .minimal
        searchBar.autocapitalizationType = .none
        return searchBar

    func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
        uiView.textual content = textual content

Leave a Reply

Your email address will not be published. Required fields are marked *