I am using the new iOS 11 searchContoller
property of a UINavigationItem
. I am running iOS 11.0 GM build.
When I perform a push segue whilst the search controller is active, it works fine. When I subsequently pop back, the search bar is collapsed and squashed up in the status bar. I cannot then cancel the search, or edit the search text.
See the following sequence of images:
The final image is showing the appearing of the table during the pop segue to return from a presented view controller back to the table with the search bar. Strangely, this doesn't always happen. It happens 90% of the time, but sometimes it behaves fine. I haven't yet worked out what I am doing differently to make it work. Once the search bar is squashed, I have to force close the app to get back to a sensible state.
The code which sets up the search controller is pretty standard. The relevant bit of viewDidLoad()
is as follows:
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.returnKeyType = .done
searchController.searchBar.placeholder = "Your Library"
searchController.searchBar.searchBarStyle = .minimal
// We will manage the clearing of selections ourselves.
clearsSelectionOnViewWillAppear = false
// Some search bar styles are slightly different on iOS 11
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationController!.navigationBar.prefersLargeTitles = true
}
else {
searchController.searchBar.backgroundColor = tableView.backgroundColor!
searchController.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.height), animated: false)
}
I've also noticed this issue in Apple's Messages app (see screenshot below), along with Settings, Notes and Mail, so presumably this is an iOS 11 bug.
This only seems to happen when using smaller than default Text Size in Settings -> General -> Accessibility -> Larger Text, and only seems to happen on a physical device (haven't reproduced it in the simulator). In viewDidAppear
, searchController.searchBar.frame.height
is equal to 0
(but not in viewDidDisappear
, not viewWillAppear
). The only workaround I have so far is:
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 11.0, *), searchController.searchBar.frame.height == 0 {
navigationItem.searchController?.isActive = false
}
super.viewDidAppear(animated)
}
Is there a better way to get around this problem?
This bug can be reproduced in iOS 11.1:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesSearchBarWhenScrolling = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (@available(iOS 11.0, *)) {
self.navigationItem.hidesSearchBarWhenScrolling = YES;
}
}
Avoiding mutate navigationItem on VC lifecycle events, fixed problem for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With