Ios UIViewController顶部带有UISearchController的UISearchBar在点击时会断开

Ios UIViewController顶部带有UISearchController的UISearchBar在点击时会断开,ios,swift,autolayout,uisearchbar,uisearchcontroller,Ios,Swift,Autolayout,Uisearchbar,Uisearchcontroller,我正在使用UISearchController及其UISearchBar 我想使用AutoLayout将UISearchBar放在我的UIViewController视图的顶部: import UIKit class ViewController: UIViewController { var searchController = UISearchController(searchResultsController: nil) let myCustomSearchBar =

我正在使用
UISearchController
及其
UISearchBar

我想使用AutoLayout将
UISearchBar
放在我的
UIViewController
视图的顶部:

import UIKit

class ViewController: UIViewController {
    var searchController = UISearchController(searchResultsController: nil)

    let myCustomSearchBar = UISearchBar()

    var searchBar: UISearchBar {
        // return searchController.searchBar
        return myCustomSearchBar
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        searchController.dimsBackgroundDuringPresentation = false
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.hidesNavigationBarDuringPresentation = false

        view.addSubview(searchBar)
        configureSearchBarLayout()
        searchBar.translatesAutoresizingMaskIntoConstraints = false
    }

    private func configureSearchBarLayout() {
        [
            searchBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            searchBar.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
            searchBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            searchBar.heightAnchor.constraint(equalToConstant: 88)
            ]
            .forEach{
                $0.isActive = true
        }
    }
}
这将导致显示以下视图:

然而,只要我点击searhBar,它就会完全移出屏幕,不再出现

如果我修改代码以包含一个基本的
UISearchBar
,而不是由
UISearchController
创建的,它可以正常工作:


当我尝试使用AutoLayout将其捕捉到顶部时,
UISearchController
的搜索栏为什么不能正常工作?如何修复它?

@ElTomato那么正确用法是什么?@ElTomato那么正确用法是什么?