Context
We’ve got an present challenge which is construct in UIKit, and now I need 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 move could be simplify like this
Challenge
However when I attempt to do a proof of idea. The code will probably 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 de facto unhealthy in expertise.
My Query
- Is there any approach to repair?
- and why it’s conduct like that?
[Update]
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"
Due to this fact, the brand new query turning into like
- Easy methods to increased backBarButtonItem ‘s compress resistance
- Easy methods to cease the backButtonTitle from hidden
I attempted to look with my new query however did not get any helpful answer. If it is exhausting to do it both with SwiftUI and UIKit, I can contemplating and persuade our designer to not present the backButtonTitle
.
Code
ViewController in UIKit
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
tremendous.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("Howdy")
}
.toolbar {
ToolbarItem(placement: .principal) {
SearchBar(textual content: .fixed("take a look at"), placeholder: "take a look at")
}
}
}
}
SearchBar
Since SwiftUI does 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
}
}