swift4搜索栏中有文字时如何使tableview不被遮挡

How to make tableview unobscured when there is text in the search bar in swift4

我有一个位于表视图控制器中的搜索控制器,代码如下

let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    ///// color of letter in search index ////
    self.tableView.sectionIndexColor = UIColor.black
    self.searchController.searchBar.delegate = self
    //////////// search Controller //////////////
    self.searchController.searchResultsUpdater = self
    //self.searchController.obscuresBackgroundDuringPresentation = true
    self.searchController.searchBar.placeholder = "Search Countries"
    self.navigationItem.searchController = searchController
    definesPresentationContext = true
    self.searchController.obscuresBackgroundDuringPresentation = true
    self.navigationItem.hidesSearchBarWhenScrolling = false



}

当我搜索搜索栏时,背景中的项目(国家列表)处于非活动状态,如图所示,这很好

我想要做的是在搜索栏中出现文本后激活背景中的项目。我尝试了以下代码,但它不起作用

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
 {
        if (searchText.count>0)
        {
            self.searchController.obscuresBackgroundDuringPresentation = false
        }
 }

我的视图控制器符合 UISearchBarDelegate 和 UISearchResultsUpdating 的信息

Apple 文档:

This property(obscuresBackgroundDuringPresentation) controls only whether the original view controller is initially obscured.

苹果的建议:

If you use the same view controller to display the searchable content and search results, it is recommended that you set this property to false. The default value of this property is true.

您可以改用 tableview 的 isUserInteractionEnabled 属性,在用户输入内容后启用它。