0
votes

I am facing an issue on the UISearchController when the device is rotated on iOS 13.

I have a tableView and this tableView has a searchBar as header. This was working properly until the iOS 13 release, now there is an unwanted behaviour on rotate device.

If the device is on portrait orientation and the searchBar is in focus then I rotate the device to landscape orientation the searchBar is not at the correct position; As you can see on the image bellow the search bar must be into the yellow view, but it is a little bit bellow it

enter image description here

but when the cancel button is pressed the search bar goes to the correct position.

enter image description here

I already debug it and the view seems to be created at the correct place, including the frame size and origin.

A similar behaviour happens when the focus is on the search bar and the orientation is in landscape then it is changed to portrait.

my code to add the search bar:

func setupSearchBar() {
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.delegate = self
    searchController.definesPresentationContext = true
    searchController.searchBar.returnKeyType = UIReturnKeyType.done
    searchController.searchBar.placeholder = "search here ..."
    searchController.searchBar.tintColor = UIColor.red
    searchController.searchBar.delegate = self

    let yellowView: UIView = UIView.init(frame: searchController.searchBar.frame)
    yellowView.backgroundColor = UIColor.yellow
    yellowView.addSubview(searchController.searchBar)

    tableView.tableHeaderView = yellowView
    tableView.reloadData()
}
1
Things work a lot better if you don't make the search bar the table view's header. Instead, put the search controller in the navigation item. See the documentation for UISearchController for details.rmaddy
Hi @rmaddy, this issue not seems to happens when the UISearchController is used on the navigation controller, but unfortunately this is not a solution for this issue, because the search bar must be hidden on the scroll of the tableView.Mailson
That's what the hidesSearchBarWhenScrolling property of UINavigationItem is for. Again, look at the documentation for UISearchController. This is all shown as an example.rmaddy
Sorry, I didn't mention it, the app is current on store and it is for iOS 9 or later. the hidesSearchBarWhenScrolling is available from iOS 11.Mailson
Just use the code for iOS 11 and later.rmaddy

1 Answers

1
votes

I solved this issue by creating a custom UISearchController.

class CustomSearchController: UISearchController {

    var _searchBar: UISearchBar = UISearchBar.init()
    override var searchBar: UISearchBar {
        get { 
            return _searchBar 
        }

        set { 
            _searchBar = newValue 
        }
    }

}



Instead of use the default UISearchBar provided by the UISearchController I override it by a new UISearchBar that I added to the tableView. 



enter image description here

func setupSearchBar() {
    searchController.searchBar = searchBar
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.delegate = self
    searchController.definesPresentationContext = true
    searchController.searchBar.returnKeyType = UIReturnKeyType.done
    searchController.searchBar.placeholder = "search here ..."
    searchController.searchBar.tintColor = UIColor.red
    searchController.searchBar.delegate = self

    tableView.tableHeaderView = searchController.searchBar
}

This solution works for iOS 9 or later and keeps the previous behaviour of the application.