swift table 搜索栏处于活动状态时视图被锁定

swift table view is locked when searchbar active

我正在使用一个包含搜索控制器的大型导航栏。如果我不搜索,我可以毫无问题地滚动浏览我的表格视图,但如果我正在搜索,它就会像锁定一样接缝。这是我的代码:

func updateSearchResults(for searchController: UISearchController) {

    // First we will check if input is only containing numbers => search for PLZ otherwise we will check if a restaurant is called like this otherwise search if there is a suitable city
    self.navigationItem.title = searchController.searchBar.text!

if !searchController.isActive{
        //TODO get all restaurants for default city
    self.navigationItem.title = "München"

    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "partnerscell", for: indexPath) as! PartnersCellTableViewCell

    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var bounds = UIScreen.main.bounds
    var width = bounds.size.width
    var height = bounds.size.height
    return height/2.2
}

@IBOutlet weak var tv: UITableView!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
    super.viewDidLoad()
            searchController.searchResultsUpdater = self
 if #available(iOS 11.0, *) {
             self.navigationController?.navigationBar.prefersLargeTitles = true
    }

    self.navigationController?.navigationBar.isTranslucent = true

    self.navigationItem.searchController = searchController
    self.navigationController?.navigationBar.shadowImage = UIImage()
}

此外,如果我正在搜索,我的工具栏看起来会更暗。请查看附件中的两个屏幕截图:

您无法访问 table 视图,因为 UISearchController 的配置错误导致它上面有一个不可见的层。 将 searchController 修改为:

let searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
searchController.definesPresentationContext = true

当您的搜索处于活动状态时,您无法访问 tableview,因为 UISearchController obscuresBackgroundDuringPresentation 的属性默认为 true,这表明当搜索处于活动状态时控制器的底层内容被遮盖了。所以你可以设置如下:

override func viewDidLoad() {
    super.viewDidLoad()
            searchController.searchResultsUpdater = self
 if #available(iOS 11.0, *) {
             self.navigationController?.navigationBar.prefersLargeTitles = true
    }

    self.navigationController?.navigationBar.isTranslucent = true

    self.navigationItem.searchController = searchController
    self.navigationController?.navigationBar.shadowImage = UIImage()
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.definesPresentationContext = true
}