嵌入在 NavigationItem 中的损坏的 UISearchBar 动画

Broken UISearchBar animation embedded in NavigationItem

我在使用向导航项添加搜索栏的新方法时遇到问题。

如下图可以看到,前后有两个UIViewController,都有搜索栏。问题是动画,当搜索栏在第一个视图控制器上可见但在第二个视图控制器上不可见时,动画很难看。搜索栏占据的区域停留在屏幕上,突然消失

代码非常基础(项目中没有做其他改动):

(我主要是用C#写的,所以这段代码可能会有错误。)

ViewController.swift:

import UIKit

class ViewController: UITableViewController, UISearchResultsUpdating {

override func loadView() {
    super.loadView()

    definesPresentationContext = true;

    navigationController?.navigationBar.prefersLargeTitles = true;
    navigationItem.largeTitleDisplayMode = .automatic;
    navigationItem.title = "VC"

    tableView.insetsContentViewsToSafeArea = true;
    tableView.dataSource = self;

    refreshControl = UIRefreshControl();
    refreshControl?.addTarget(self, action: #selector(ViewController.handleRefresh(_:)), for: UIControlEvents.valueChanged)
    tableView.refreshControl = refreshControl;

    let stvc = UITableViewController();
    stvc.tableView.dataSource = self;

    let sc = UISearchController(searchResultsController: stvc);
    sc.searchResultsUpdater = self;
    navigationItem.searchController = sc;
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell1");
    if (cell == nil) {
        cell = UITableViewCell(style: .default, reuseIdentifier: "cell1");
    }
    cell?.textLabel?.text = "cell " + String(indexPath.row);
    return cell!;
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20;
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = ViewController();
    navigationController?.pushViewController(vc, animated: true);
}

@objc func handleRefresh(_ refreshControl: UIRefreshControl) {
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
        refreshControl.endRefreshing();
    })
}

func updateSearchResults(for searchController: UISearchController) {
}
}

AppDelegate.swift:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds);
    window?.rootViewController = UINavigationController(rootViewController: ViewController());
    window?.makeKeyAndVisible();

    UINavigationBar.appearance().barTintColor = UIColor.red;

    return true
}
}

想法?

看来 Apple 仍需解决在新的大标题样式中使用 UISearchBar 的问题。如果您推送到的 UIViewController 没有设置 navigationItem.searchController,则动画效果很好。在两个都设置了 searchController 的 UIViewController 实例之间导航时,您会遇到描述导航栏高度跳跃位置的问题。

您可以通过在每次调用 viewDidAppear 时创建 UISearchController(而不是在 loadView 中创建)并设置 navigationItem.searchController 来解决(解决)问题在 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
}

异步调度的原因是在viewDidAppear方法中设置navigationItem.searchController内联时,抛出异常:

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>>'

我知道这只是一种变通方法,但希望这对您暂时有帮助,直到 Apple 解决了在两个视图控制器之间导航的问题,这两个视图控制器的 navigationItem 都设置了 UISearchController ].

我在 viewDidLoad() 中添加了这段代码,当我移入选项卡的 b/w 时,它正在运行

searchController.dimsBackgroundDuringPresentation

我对这个问题的解决方案是在 UIViewController 被取消时更新保持 UISearchBar 可见的约束。我无法使用 silicon_valley 的解决方案,因为即使使用异步调度我也遇到了他提到的崩溃。诚然,这是一个非常混乱的解决方案,但 Apple 并没有让它变得简单。

下面的代码假定您有一个 属性,其中包含 UIViewController 子类中的一个 UISearchController 实例,名为 searchController

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    if
        animated
            && !searchController.isActive
            && !searchController.isEditing
            && navigationController.map({[=10=].viewControllers.last != self}) ?? false,
        let searchBarSuperview = searchController.searchBar.superview,
        let searchBarHeightConstraint = searchBarSuperview.constraints.first(where: {
            [=10=].firstAttribute == .height
                && [=10=].secondItem == nil
                && [=10=].secondAttribute == .notAnAttribute
                && [=10=].constant > 0
        }) {

        UIView.performWithoutAnimation {
            searchBarHeightConstraint.constant = 0
            searchBarSuperview.superview?.layoutIfNeeded()
        }
    }
}

您可以删除 performWithoutAnimationlayoutIfNeeded,它仍然会显示动画;但是我发现动画从来没有第一次触发过,而且看起来也不是那么好。

我希望 Apple 在以后的 iOS 版本中解决这个问题,在撰写本文时当前版本是 12.1.4。

确实解决了某些情况下的问题,但我遇到过它导致在第一个搜索栏处于活动状态时完全删除推送视图控制器中的 navigationItem

我想出了另一个解决方法,类似于 ,但不需要干预约束。该方法是在转场点确定搜索栏是否可见。如果是,我们指示目标视图控制器使其搜索栏在加载时可见。这意味着导航项动画的行为正确:

假设两个view controller分别叫UIViewController1UIViewController2,其中1压入2,代码如下:

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
    }
}

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
    }
}

VC2中使用相同的代码。

这将比将 serchController 设置为 nil

更清楚地解决您的问题

此问题已在 iOS 13 beta 1 中修复。在为新版本更新应用程序之前检查您的解决方法。

在花了几天时间解决和调查后,我遇到了同样的问题。最后,我创建了一个自定义视图,并用这个视图替换了 titleView。 这意味着,我不使用搜索控制器。 iOS 11 不好,Apple。