IOS - UISearchController - defines the prefix layout job

I have UIViewControllerwith a navigation bar HIDDEN, a few buttons up and UITableViewwith UISearchControlleras a title. Here is the problem: when I create UISearchController, I also have this line:

    self.definesPresentationContext = YES;

Now, when searching with UISearchControllerand click one of the results in UITableView, it will open my next one UIViewController(this is exactly what it should do - and the next one UIViewControlleralso has a hidden navigation bar), but it displays a grayish navigation bar at the top UIViewController, although I set the navigation bar as hidden.

Now when I install:

    self.definesPresentationContext = NO;

The navigation bar does not appear in the next view, but instead UISearchController SearchBarappears in the next UIViewControllerin the same place as in the main view controller, although this obviously should not be there anymore.

Here's what it should look like (top of the UIViewController): enter image description here

This is what happens when `self.definesPresentationContext = YES;

enter image description here

And this is what happens when `self.definesPresentationContext = NO;

enter image description here

How can I get back to number one? UPDATE Here is an example project that duplicates this issue: http://www.filedropper.com/sampleprojectbugreport

+4
source share
2 answers

iOS. , UINavigationController -setNavigationBarHidden:animated: hidden:

-(void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
    [super setNavigationBarHidden:YES animated:animated];
}

, .

. : http://appsandwich.com/stackoverflow/navcontrollersubclass.zip

+2

. KVO. , viewWillAppear (_:), navigationBar isHidden . viewDidAppear (_:), navigationBar isHidden false. , . , KVO.

viewWillAppear (_:)

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.navigationBar.addObserver(self, forKeyPath: "hidden", options: [.new], context: nil)
}

navigationBar isHidden observerValue()

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    guard let keyPath = keyPath else { return }    
    if keyPath == "hidden" {
        // you should remove the KVO before set
        navigationController?.navigationBar.removeObserver(self, forKeyPath: "hidden")
        navigationController?.navigationBar.isHidden = true
    } else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }
}

, . , .

0

Source: https://habr.com/ru/post/1626288/


All Articles