我对UISearchBar
有疑问。当我只使用UISearchBar
时,它可以很好地工作。但我想使用Cancel
按钮和其他UISearchController
的功能。
不幸的是,我的SearchViewController
必须在弹出窗口中显示(我正在使用Presentr
CocoaPod)。
仅当我单击UISearchBar
时才会出现问题。
它只是简单地向左移动。由于Presenter的弹出框,因此已被剪掉。
这是代码:
class SearchViewController: UIViewController {
private let tableView = UITableView()
private let searchController = UISearchController(searchResultsController: nil)
private let doneButton = UIButton(type: .system)
private var data: [String]?
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
setupViews()
}
init(data: [String]) {
self.data = data
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupViews() {
view.addSubview(searchController.searchBar)
searchController.searchBar.snp.makeConstraints { make in
make.left.right.top.equalToSuperview()
}
view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.top.equalTo(searchController.searchBar.snp.bottom)
make.bottom.right.left.equalToSuperview()
}
extendedLayoutIncludesOpaqueBars = true
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search your country code"
tableView.tableFooterView = doneButton
definesPresentationContext = true
automaticallyAdjustsScrollViewInsets = false
}
}
请忘记doneButton
,它尚未使用。