Navigation bar issue when search is active and push to next view controller
Asked Answered
R

3

9

I am facing issue with navigation bar. I'm adding searchController in navigationItem's search controller.

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. This is working fine. (default navigation bar is hidden)

2) Now, I have clicked on search bar and then click on table view cell. 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:

self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
Rambort answered 18/7, 2018 at 12:54 Comment(2)
how are you hiding the navBar and whats the code for your tableView(_ tableView: didSelectRowAt indexPath: )?Undone
@zero3nna, Please see my update question, I did add code that hide navigation bar.Rambort
A
3

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.

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.

Achondroplasia answered 25/7, 2018 at 15:44 Comment(2)
This work for me..but search state is not maintain..Is it possible to maintain search state as well?Shun
Yes, you have to store previous search text and when come back, just simply assign it to search textfield and reload table if there are data.Achondroplasia
E
10

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().

I solved it by hide the navigation bar again in viewWillLayoutSubviews():

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    navigationController?.setNavigationBarHidden(true, animated: false)
}
Edom answered 24/10, 2019 at 11:46 Comment(3)
I just added this (not even in viewWillAppear, just viewWillLayoutSubviews) and it works perfectly! Thanks Dave :)Bestir
I've tried viewDidLoad, viewWillAppear, viewDidAppear. I've tried adding custom navigation bar on search view. Nothing works. 3 days later, I found this answer. Thanks a lot!Maidel
This should be the accepted answer as this solution means the searchController can remain active in the previous view if that is the required functionality.Fino
A
3

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.

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.

Achondroplasia answered 25/7, 2018 at 15:44 Comment(2)
This work for me..but search state is not maintain..Is it possible to maintain search state as well?Shun
Yes, you have to store previous search text and when come back, just simply assign it to search textfield and reload table if there are data.Achondroplasia
P
3

Objective-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()
    }
}
Plumbum answered 29/10, 2019 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.