SearchBar没有出现在Swift 4 IOS 11的NavigationBar中

Ach*_*les 8 swift4 ios11 xcode9

我一直在尝试通过我的应用程序中的UISearchController添加Searchbar.它没有出现在我的导航栏上.我添加了一个导航控制器作为根视图控制器,并没有改变任何东西,只是它的色调颜色.我尝试了一些解决方案但没有任何效果.我正在使用Swift 4.0和Xcode 9并在IOS 11上运行应用程序.下面是我的代码.提前致谢.

if #available(iOS 11.0, *) {
        let sc = UISearchController(searchResultsController: nil)
        sc.delegate = self
        let scb = sc.searchBar
        scb.sizeToFit()

        scb.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
        scb.tintColor = UIColor.white


        self.navigationController?.navigationItem.searchController = sc
        self.navigationController?.navigationItem.hidesSearchBarWhenScrolling = false

    }
Run Code Online (Sandbox Code Playgroud)

JD.*_*JD. 6

我不知道为什么,但是它将为您提供帮助。

直接在NavigationItem中添加搜索控制器。

navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
Run Code Online (Sandbox Code Playgroud)