Uisearchcontroller Searchbar Misaligns While Active During Rotation

UISearchController SearchBar misaligns while active during rotation

So after so many days of struggling with this:

Portrait to Landscape

  1. Make SearchBar/SearchController active in Portrait
  2. Rotate to Landscape
  3. Press Cancel and SearchBar will go back to Portrait width

Landscape to Potrait

  1. Make SearchBar/SearchController active in Landscape
  2. Rotate to Portrait
  3. 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

screenshot



Related Topics



Leave a reply



Submit