UISearchController Not Redisplaying Navigation Bar on Rotate
It looks like the UISearchController
forgets to reset the frame of the searchBar
when the status bar reappears. I think this is probably a bug in UISearchController
; there seem to be a few listed in radar. It seems the searchBar's superview (which is internal to the UISearchController) ends up with the wrong height. This is vexing since the solution therefore involves reaching into the searchController's view hierarchy, which Apple could change... you might want to add a check of the iOS version so it only runs for specified versions.
If you add the code below to your view controller, it will be called when the trait collection changes. It checks to see a) the search controller is active, b) the statusBar is not hidden, and c) the searchBar origin-y is 0, and if so it increases the height of the superview by the height of the statusBar, which moves the searchBar down.
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
let app = UIApplication.sharedApplication()
if searchController!.active && !app.statusBarHidden && searchController?.searchBar.frame.origin.y == 0 {
if let container = self.searchController?.searchBar.superview {
container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y, container.frame.size.width, container.frame.size.height + app.statusBarFrame.height)
}
}
}
Objective C
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
[super traitCollectionDidChange: previousTraitCollection];
if(self.venueSearchController.active && ![UIApplication sharedApplication].statusBarHidden && self.venueSearchController.searchBar.frame.origin.y == 0)
{
UIView *container = self.venueSearchController.searchBar.superview;
container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y, container.frame.size.width, container.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height);
}
}
How to prevent the UISearchController from showing the navigation bar?
Using a navigation bar that is not the standard maybe it's not the best idea. (I know customers can sometimes be stubborn, but we should teach them that sometimes standard solutions have a lot of good points, like low maintenance for example, which turns in lower bills for them).
Having said that, as a last resort I may suggest you a quite "strong" approach.
You could subclass the UINavigationController with a custom class, and inside this class you could override the setNavigationBarHidden method like this
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated{
[super setNavigationBarHidden:YES animated:NO];
}
method. This should make the bar hidden all the time.
Still, i'm not a big supported of this kind of solutions, but it may work in your case.
iOS8 Swift UISearchController hides navigationbar
The UISearchController has a property called hidesNavigationBarDuringPresentation
, maybe that can help you.
EDIT: Just tried it myself and it works, just add this line:
searchController.hidesNavigationBarDuringPresentation = false
Related Topics
Why Is Swiftui Picker in Form Repositioning After Navigation
Ios11 Causing Cors Issues in All Mobile Browsers
In Os X 10.10 (Yosemite Beta), How to Test Using iOS 6.1 Simulator
Xcode 8 Swift 3 Uitableview Space Between Cells
Apple MACh-O Linker & Ditto Error - Xcode 8
How to Access iOS Private APIs in Swift
Override Uiappearance Property for Mfmailcomposeviewcontroller
iOS 8+ Framework with Nested Embedded Framework
Embedding a Framework Within a Framework (iOS 8+)
Uicollectionview - iOS 10 - Crash on iPhone 6 Plus Simulator But Works on Real Device
External Framework File/File.H (Parse/Parse.H) File Not Found
Status Bar Visible on iPad Mini Despite Setting Uiviewcontrollerbasedstatusbarappearance to No
Share Image with Hashtag via Uiactivityviewcontroller (Twitter, Facebook, Instagram)
Domain=Nsurlerrordomain Code 1202
What the Meaning of Question Mark '' in Swift
iOS 7 Device Token Is Different for Same Device
How to Set Accessibility Identifier in Interface Builder? Xcode4.2