Reputation: 3246
I have a UISearchBar
attached to the UINavigationController
and when I tap the back button of the presented UIViewController
which contains the UISearchBar
it doesn't disappear as I expected, it stays still attached to the nav bar and appears on the parent view controller.
Here's my UISearchBar
declaration:
lazy var searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Search"
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230) // cor de fundo da search bar
sb.delegate = self
return sb
}()
Here's how I attach the UISearchBar
to the UINavigationController
navigationController?.navigationBar.addSubview(searchBar)
I tried ti call searchBar.removeFromSuperview()
on the viewWillDisappear
but it didn't worked.
Any hint?
Thank you
Upvotes: 1
Views: 202
Reputation: 20804
Use self.navigationItem.titleView = searchBar
for adding the search bar instead of navigationController?.navigationBar.addSubview(searchBar)
and when you need to remove it then replace the titleView by another UIView
or make it = nil
to Add
self.navigationItem.titleView = searchBar
to Remove
self.navigationItem.titleView = UIView()
Upvotes: 1