简体   繁体   中英

UISearchController not working properly on iOS 13

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

在此处输入图像描述

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

在此处输入图像描述

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()
}

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.

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM