Xcode 10.1,Swift 4.2
ViewController A具有 LargeTitles , UISearchController 和 UITableView 。
在选择TableViewCell后推动ViewController B(也就是TableView)时,如动画所示,UISearchBar所占用的空间存在延迟。
ViewController B也有一个UISearchController,但是按照通常的行为,它是隐藏的,直到用户在屏幕上向下拖动为止。
有人知道如何防止这种情况发生吗?
其他信息和代码:
ViewController A 轻按单元格时,它会使用情节提要中的 Show(例如Push)选择来推送ViewController B,并选择 Animates 。使用 prepareForSegue 方法注入数据。
performSegue(withIdentifier: "showLogbook", sender: self)
ViewController B:
class LogbookVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchResultsUpdating {
//Extract
private let searchController = UISearchController(searchResultsController: nil)
@IBOutlet private var addButton: UIBarButtonItem!
@IBOutlet private var shareButton: UIBarButtonItem!
@IBOutlet private var backButton: UIBarButtonItem!
@IBOutlet private var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = 75.0
//loading tableView data
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = true
self.definesPresentationContext = true
configureSearchController()
fetchAndSortLogEntries()
}
private func configureSearchController() {
navigationItem.searchController = searchController
searchController.searchResultsUpdater = self
searchController.searchBar.scopeButtonTitles = ["Newest", "Oldest"]
searchController.searchBar.delegate = self
searchController.searchBar.barStyle = .black
searchController.searchBar.tintColor = K.Colors.appMid
searchController.searchBar.keyboardAppearance = .dark
searchController.searchBar.returnKeyType = .done
searchController.dimsBackgroundDuringPresentation = false
let attributes = [NSAttributedString.Key.foregroundColor: K.Colors.appMid]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
searchController.searchBar.setValue("Reset", forKey: "_cancelButtonText")
let searchBarCancelButton = searchController.searchBar.value(forKey: "cancelButton") as? UIButton
searchBarCancelButton?.setTitle("Reset", for: .normal)
}
答案 0 :(得分:0)
我认为当使用LargeTitles
并在相邻的UISearchControllers
中有两个ViewControllers
时,这是一个错误。
我发现通过将UISearchController
添加到NavigationItem
中的viewDidAppear
可以解决此问题,但是必须异步添加,否则会发生以下致命错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Only one palette with a top boundary edge can be active outside of a transition.
此代码需要同时添加到ViewControllers
中,以便在导航回到ViewController A时也可以防止出现问题。
Xcode 10.1,Swift 4.2
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.navigationItem.searchController = self.searchController
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
}
}
此代码将确保下一个NavigationBar
出现时ViewController
不被隐藏:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.isNavigationBarHidden = false
}