测试您描述的导航项和搜索栏设置,我只能在过渡动画期间重现 1px/point 线。
解决方法:此行实际上是一个背景布局视图( forLastBaselineLayout
),UINavigationBar
由于UISearchBar
. 设置其背景以匹配您的导航栏颜色以将其隐藏在后站点中
// MARK: UINavigationControllerDelegate
public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.navigationController?.navigationBar
.forLastBaselineLayout.backgroundColor = .red // TODO:
}
确保将对象实现UINavigationControllerDelegate
设置为navigationController.delegate
接收上述委托调用。
另外(不是问题的一部分): UISearchBar
在控制器之间转换(推送和弹出)时似乎表现得很奇怪,尤其是当一个有/显示一个searchBar
但没有另一个时。解决方法,我发现暂时隐藏搜索栏以显示控制器在视觉上更令人愉悦。这是我使用的代码:
public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if let sender = navigationStack.popLast() {
prepareSearchBarForTransition(from: sender)
}
navigationStack = navigationController.children
}
/// Set only from `navigationController: willShow`
private var navigationStack: [UIViewController] = []
func prepareSearchBarForTransition(from sender: UIViewController) {
if #available(iOS 11.0, *) {
let searchController = sender.navigationItem.searchController
sender.navigationItem.searchController = nil
weak var weakSender = sender
let navigationTransitionDuration: TimeInterval = 0.33
DispatchQueue.main.asyncAfter(deadline: .now() + navigationTransitionDuration) {
/// Reset the search bar.
weakSender?.navigationItem.searchController = searchController
}
} else {
// TODO: Check if the above workaround is neccessary for < iOS 11
}
}