与UINavigationItem一起使用时,SearchResultsController会遮挡屏幕

时间:2018-05-02 14:24:21

标签: ios uisearchbar viewcontroller uisearchcontroller navigationcontroller

我使用iOS 11中提供的新集成extension String { func runesRangeToNSRange(from: Int, to: Int) -> NSRange { let length = to - from let start = unicodeScalars.index(unicodeScalars.startIndex, offsetBy: from) let end = unicodeScalars.index(start, offsetBy: length) let range = start..<end return NSRange(range, in: self) } } ,问题是我点击搜索栏并开始输入文字时,UISearchBar会遮挡整个屏幕,包括搜索栏。之后不可能解雇结果控制器或取消搜索。

为了证明这个问题,我已经配置了一个可重复性最小的例子:

SearchResultsController
  1. 初始状态 1
  2. 点击SearchBar 2
  3. 屏幕被import UIKit let reuseid = "reuseIdentifier" class TableViewController: UITableViewController, UISearchResultsUpdating { override func viewDidLoad() { super.viewDidLoad() tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseid) } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: reuseid, for: indexPath) return cell } func updateSearchResults(for searchController: UISearchController) { print(searchController.searchBar.text) } } class ViewController: UIViewController { var searchController: UISearchController! override func viewDidLoad() { super.viewDidLoad() searchController = UISearchController(searchResultsController: TableViewController()) navigationItem.searchController = searchController } } @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) window?.backgroundColor = UIColor.white window?.makeKeyAndVisible() let nc = UINavigationController(rootViewController: ViewController()) window?.rootViewController = nc return true } } 遮挡 - 绿色 3
  4. 什么可能导致此错误以及如何避免?

1 个答案:

答案 0 :(得分:0)

通过将此添加到SearchResultsUpdater

来解决
edgesForExtendedLayout = []

使用默认边缘时,ResultsController会尝试将其视图扩展到UINavigationBar之外,因此会模糊它:

enter image description here