Broken UISearchBar animation embedded in NavigationItem

前端 未结 7 653
孤城傲影
孤城傲影 2020-12-01 03:01

I am experiencing a problem with the new way of adding search bar to the navigation item.

As you can see in the picture below, there are two UIViewControllers one af

相关标签:
7条回答
  • 2020-12-01 03:37

    This issue was fixed in iOS 13 beta 1. Check your workarounds before update app for new verison.

    0 讨论(0)
  • 2020-12-01 03:41

    The accepted answer does solve the problem for some situations, but I was experiencing it resulting in the complete removal of the navigationItem in the pushed view controller if the first search bar was active.

    I've come up with another workaround, similar to the answer by stu, but requiring no meddling with constraints. The approach is to determine, at the point of the segue, whether the search bar is visible. If it is, we instruct the destination view controller to make its search bar visible from load. This means that the navigation item animation behaves correctly:

    Assuming the two view controllers are called UIViewController1 and UIViewController2, where 1 pushes 2, the code is as follows:

    class ViewController1: UITableViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let searchController = UISearchController(searchResultsController: nil)
            searchController.obscuresBackgroundDuringPresentation = false
            navigationItem.searchController = searchController
    
            definesPresentationContext = true
        }
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let viewController2 = segue.destination as? ViewController2, let searchController = navigationItem.searchController {
    
                // If the search bar is visible (but not active, which would make it visible but at the top of the view)
                // in this view controller as we are preparing to segue, instruct the destination view controller that its
                // search bar should be visible from load.
                viewController2.forceSearchBarVisibleOnLoad = !searchController.isActive && searchController.searchBar.frame.height > 0
            }
        }
    }
    
    class ViewController2: UITableViewController {
    
        var forceSearchBarVisibleOnLoad = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let searchController = UISearchController(searchResultsController: nil)
            searchController.obscuresBackgroundDuringPresentation = false
            navigationItem.searchController = searchController
    
            // If on load we want to force the search bar to be visible, we make it so that it is always visible to start with
            if forceSearchBarVisibleOnLoad {
                navigationItem.hidesSearchBarWhenScrolling = false
            }
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            // When the view has appeared, we switch back the default behaviour of the search bar being hideable.
            // The search bar will already be visible at this point, thus achieving what we aimed to do (have it
            // visible during the animation).
            navigationItem.hidesSearchBarWhenScrolling = true
        }
    }
    
    
    0 讨论(0)
  • 2020-12-01 03:41

    I got the same issue, after spent some days to workaround and investigate. Finally, I create a custom view, and replace the titleView by this view. It means, I don't use the search controller. iOS 11 is a bad, Apple.

    0 讨论(0)
  • 2020-12-01 03:51

    It looks like Apple still needs to iron out the use of the UISearchBar in the new large title style. If the UIViewController you push to doesn't have its navigationItem.searchController set, the animation works fine. When navigating between two instances of UIViewController that both have a searchController set, you get the issue you describe where the height of the navigation bar jumps.

    You can solve (work around) the problem by creating the UISearchController every time viewDidAppear gets called (instead of creating it in loadView) and setting navigationItem.searchController to nil on viewDidDisappear.

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        DispatchQueue.main.async {
            let stvc = UITableViewController()
            stvc.tableView.dataSource = self
    
            let sc = UISearchController(searchResultsController: stvc)
            sc.searchResultsUpdater = self
            self.navigationItem.searchController = sc
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    
        self.navigationItem.searchController = nil
    }
    

    The reason for the asynchronous dispatch is that when setting the navigationItem.searchController inline in the viewDidAppear method, an exception is raised:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Only one palette with a top boundary edge can be active outside of a transition. Current active palette is <_UINavigationControllerManagedSearchPalette: 0x7fad67117e80; frame = (0 116; 414 0); layer = <CALayer: 0x60400002c8e0>>'
    

    I know this is only a work around, but hopefully this will help you for now, until Apple solves the issue with navigating between two view controllers that both have a UISearchController set on their navigationItem.

    0 讨论(0)
  • 2020-12-01 03:52

    My solution for this problem is to update the constraint that is keeping the UISearchBar visible, when the UIViewController is being dismissed. I was not able to use silicon_valley's solution as even with the asynchronous dispatch I was getting the crash he mentioned. This is admittedly a pretty messy solution but Apple hasn't made this easy.

    The code below assumes you have a property containing a UISearchController instance within your UIViewController subclass called searchController

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        if
            animated
                && !searchController.isActive
                && !searchController.isEditing
                && navigationController.map({$0.viewControllers.last != self}) ?? false,
            let searchBarSuperview = searchController.searchBar.superview,
            let searchBarHeightConstraint = searchBarSuperview.constraints.first(where: {
                $0.firstAttribute == .height
                    && $0.secondItem == nil
                    && $0.secondAttribute == .notAnAttribute
                    && $0.constant > 0
            }) {
    
            UIView.performWithoutAnimation {
                searchBarHeightConstraint.constant = 0
                searchBarSuperview.superview?.layoutIfNeeded()
            }
        }
    }
    

    You can remove the performWithoutAnimation and layoutIfNeeded, and it will still animate; however I found the animation was never triggered the first time, and it doesn't look that great anyway.

    I hope Apple fixes this in a later iOS release, the current release is 12.1.4 at the time of writing.

    0 讨论(0)
  • 2020-12-01 03:56

    In the VC1:

    override func viewDidLoad() {
       if #available(iOS 11.0, *) {
            navigationItem.hidesSearchBarWhenScrolling = false
            navigationItem.searchController = searchController
        }
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if #available(iOS 11.0, *) {
            navigationItem.hidesSearchBarWhenScrolling = true
        }
    }
    

    In the VC2, use the same code.

    This will solve your problem more clearly, than set serchController to nil

    0 讨论(0)
提交回复
热议问题