UISearchController SearchBar misaligns while active during rotation
So after so many days of struggling with this:
Portrait to Landscape
- Make SearchBar/SearchController active in Portrait
- Rotate to Landscape
- Press Cancel and SearchBar will go back to Portrait width
Landscape to Potrait
- Make SearchBar/SearchController active in Landscape
- Rotate to Portrait
- Press Cancel and SearchBar will go back to Landscape width
I finally solved it on my own. It seems that when a user presses Cancel on the SearchBar, the ViewController will call viewDidLayoutSubviews
thus I tried to reset the width by calling this function in viewDidLayoutSubviews
:
func setupSearchBarSize(){
self.searchController.searchBar.frame.size.width = self.view.frame.size.width
}
But that didn't work as well as I thought. So here is what I think happens. When a user activates the SearchController/SearchBar, the SearchController saves the current frame before it resizes itself. Then when a user presses Cancel or dismisses it, it will use the saved frame and resize to that frame size.
So in order to force its width to be realigned when I press Cancel, I have to implement the UISearchControllerDelegate in my VC and detect when the SearchController is dismissed, and call setupSearchBarSize()
again.
Here are the relevant codes to solve this question:
class HomeMainViewController : UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var searchContainerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.autoresizingMask = .FlexibleRightMargin
searchController.searchBar.delegate = self
searchController.delegate = self
definesPresentationContext = true
self.searchContainerView.addSubview(searchController.searchBar)
setupSearchBarSize()
}
func setupSearchBarSize(){
self.searchController.searchBar.frame.size.width = self.view.frame.size.width
}
func didDismissSearchController(searchController: UISearchController) {
setupSearchBarSize()
}
override func viewDidLayoutSubviews() {
setupSearchBarSize()
}
}
UISearchController searchBar disappears on first click
Try to check the navigationBar.translucent
property - it should be YES when UISearchController
will present the searchBar
or else will be UI bugs.
Update from @SiavA
The better solution is use the extendedLayoutIncludesOpaqueBars
property of the UIViewController
. If you using the opaque navigation bar just set it in the true
for controller which will be show UISearchController
(not for navigationController
).
E.g.
- (void)viewDidLoad {
[super viewDidLoad];
self.extendedLayoutIncludesOpaqueBars = !self.navigationController.navigationBar.translucent;
}
Presenting UISearchController overlaps with statusbar
I have solved the issue by remove the following line:
self.definesPresentationContext = true
The reason this is not required in my scenario is that I am calling self.present(self.searchController, animated: true, completion: nil)
UPDATE:
While the above did fix my issue, it broke the functionality of pushing a viewController in my navigationController while the SearchController was displayed (the searchController would stay on top while the viewController was pushed underneath it)
Upon revisiting the issue, here are the fixes I made:
Add the following
searchController.hidesNavigationBarDuringPresentation = true
self.definesPresentationContext = true
Then in the storyboard enable "Under Top Bars" and enable "Under Opaque Bars"
SearchBar rotation problem
Verify all your views frames in:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
And if they are not ok set the correct frame.
iOS 9 searchBar disappears from table header view when UISearchController is active
It seems all of us got the same problem but they were solved in different ways. However none of the suggested answers worked for me :(. Nevertheless thank you all for your time.
I got a solution that solved my problem. It is setting Extend Edges - Under Opaque Bars of my (MyModalView: UITableViewController) to true in the Storyboard using Interface Builder.
In summary:
MyModalView: UITableViewController, in Storyboard using Interface Builder has
Extend Edges:
- Under Top Bars ticked
- Under Bottom Bars ticked
- Under Opaque Bars ticked
Related Topics
Nstextfield Non-System-Font Content Clipped When Usessinglelinemode Is True
Cmlogitem Timestamp: Why So Complicated
Issues Scaling Sprite Width to Screen Width
Enum of String Type Vs Struct with Static Constant
Dependency Injection in View Controller
How to Restore In-App Purchases Correctly
Swift Type Inference and Type Checking Issue
Alamofire Https Change in 10.3
How to Make a Function to Accept Any Enum Types That Have a Rawvalue of String
Remove from Array of Anycancellable When Publisher Finishes
How to Access The Firebase Topics a User Is Subscribed To
Override UIgesturerecognizer Touchesbegan
How to Detect If The User Was Deleted from Firebase Auth
What's The Difference Between [String] VS. [(String)]
Arkit - Raycasting Using a World Ray Instead of a Screen Point