问题
I am having trouble using navigationItem.searchController
in iOS 13.
The search bar of the search controller is initially closed, but it won't collapse back after I scroll the table view.
It 'bounces' back when I try to hide the bar.
Also, the navigation bar turns white after the search bar appears. (The navigation bar has a custom tint color; see the attached screenshots.) Note that this wasn't the case in iOS < 13.
Is this a bug in iOS 13? If it is, is there a work around to keep the tint color of the navigation bar and make the search bar hide when the table view is scrolled upwards?
The search bar seems to hide when the table view is populated with cells to fill the screen.
The following is the code for my ViewController
, which is embedded in a UINavigationController
:
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = UISearchController(searchResultsController: nil)
}
}
Screenshots:
EDIT:
I managed to 'fix' the new behavior using the new UINavigationBarAppearance
API, but I still cannot get the original translucent look of the navigation bar and making it collapse.
UINavigationBar.appearance().isTranslucent = true
doesn't seem to work along with the new API, so I just adjusted the color to match the translucent version using a color picker.
Also, there does not seem to be a way to collapse the search bar properly.
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
-> Bool
{
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .redAdjusted // adjusted to match the translucent version
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = .red
UINavigationBar.appearance().isTranslucent = true
}
}
}
来源:https://stackoverflow.com/questions/58780350/uinavigationbar-turns-white-and-wont-close-when-using-navigationitem-searchcont