与 UINavigationItem 一起使用时,SearchResultsController 会遮挡屏幕
SearchResultsController obscures screen when used with UINavigationItem
我正在使用 iOS 11 中可用的新集成 UISearchBar
,问题是当我点击搜索栏并开始输入文本时,SearchResultsController
遮盖了整个屏幕,包括搜索栏。之后无法关闭结果控制器或取消搜索。
为了演示这个问题,我配置了一个最小的可重现示例:
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
}
}
- 初始状态
- 点击搜索栏
- 屏幕被
UITableView
遮挡 - 颜色为绿色
什么会导致这个错误,如何避免?
通过将此添加到 SearchResultsUpdater
解决:
edgesForExtendedLayout = []
使用默认边,ResultsController
试图将其视图扩展到 UINavigationBar
之外,因此遮盖了它:
我正在使用 iOS 11 中可用的新集成 UISearchBar
,问题是当我点击搜索栏并开始输入文本时,SearchResultsController
遮盖了整个屏幕,包括搜索栏。之后无法关闭结果控制器或取消搜索。
为了演示这个问题,我配置了一个最小的可重现示例:
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
}
}
- 初始状态
- 点击搜索栏
- 屏幕被
UITableView
遮挡 - 颜色为绿色
什么会导致这个错误,如何避免?
通过将此添加到 SearchResultsUpdater
解决:
edgesForExtendedLayout = []
使用默认边,ResultsController
试图将其视图扩展到 UINavigationBar
之外,因此遮盖了它: