[英]Navigation bar issue when search is active and push to next view controller
I am facing issue with navigation bar.我正面临导航栏的问题。 I'm adding searchController
in navigationItem's search controller.我在 navigationItem 的搜索控制器中添加searchController
。
See the images on following link: navigation bar issue请参阅以下链接上的图片:导航栏问题
Steps:脚步:
1) I have data in table view, when I click on cell it's open details screen with custom navigation view. 1)我在表格视图中有数据,当我点击单元格时,它是带有自定义导航视图的打开详细信息屏幕。 This is working fine.这工作正常。 (default navigation bar is hidden) (默认导航栏是隐藏的)
2) Now, I have clicked on search bar and then click on table view cell. 2)现在,我点击了搜索栏,然后点击了表格视图单元格。 It's show me default navigation bar to details screen.它向我显示了详细信息屏幕的默认导航栏。 I don't want to display default navigation bar.我不想显示默认导航栏。
Code that I did write to implement search controller is as follows:我为实现搜索控制器而编写的代码如下:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.tintColor = .white
searchController.searchBar.barTintColor = .white
if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
self.navigationItem.searchController = self.searchController
definesPresentationContext = true
Below is code to hide navigation bar inside didSelect method:下面是在 didSelect 方法中隐藏导航栏的代码:
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
I had the same problem.我有同样的问题。 It seems that something make the navigation bar visible when search controller was active in previous view although I hide the navigation bar in viewWillAppear().尽管我在 viewWillAppear() 中隐藏了导航栏,但当搜索控制器在前一个视图中处于活动状态时,似乎有些东西使导航栏可见。
I solved it by hide the navigation bar again in viewWillLayoutSubviews():我通过在 viewWillLayoutSubviews() 中再次隐藏导航栏来解决它:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
navigationController?.setNavigationBarHidden(true, animated: false)
}
You can fix this issue by making search controller inactive, then navigate to your details view controller after some delay.您可以通过使搜索控制器处于非活动状态来解决此问题,然后在一些延迟后导航到您的详细信息视图控制器。
Try following code in you didSelect
method which will help you to hide navigation bar when search controller is active.尝试在didSelect
方法中使用以下代码,这将帮助您在搜索控制器处于活动状态时隐藏导航栏。
searchController.isActive = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
self.navigationController?.pushViewController(<YourViewController>, animated: true)
}
You must required delay to navigate otherwise it's give you warning in console regarding navigation controller presentation process.您必须需要延迟才能导航,否则它会在控制台中向您发出有关导航控制器呈现过程的警告。
So, this code first make your search controller inactive and then navigate to your next view controller.因此,此代码首先使您的搜索控制器处于非活动状态,然后导航到您的下一个视图控制器。
I hope this is helpful to you.我希望这对你有帮助。
IF any one have same issue then please try this solution..如果有人遇到同样的问题,请尝试此解决方案。
It's working for me.它对我有用。 I hope it's working for you too.我希望它也对你有用。
Objective-C目标-C
-(void)viewWillDisappear:(BOOL)animated{
if (@available(iOS 13.0, *)) {
[self.navigationController.view setNeedsLayout];
[self.navigationController.view layoutIfNeeded];
}
}
Swift迅速
func viewWillDisappear(_ animated: Bool) {
if (@available(iOS 13.0, *)) {
self.navigationController?.view.setNeedsLayout()
self.navigationController?.view.layoutIfNeeded()
}
}
You could implement this in your detail viewController if you want the default navBar to be hidden at any time: 如果要随时隐藏默认的navBar,则可以在详细信息viewController中实现此功能:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.isNavigationBarHidden = true
// or like so
navigationController?.setNavigationBarHidden(true, animated: false)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.