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

标签 ios animation uisearchbar ios11

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

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

Demo

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

(我主要是用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 中创建它)并在 viewDidDisappear 上将 navigationItem.searchController 设置为 nil。

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 解决了在两个 View Controller 之间导航的问题,这两个 View Controller 的 都设置了 UISearchController导航项

关于ios - 嵌入在 NavigationItem 中的损坏的 UISearchBar 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46301813/

相关文章:

android - 翻译动画部分的屏幕变为空白

ios - Swift 4.2 中导航栏 Searchbar Space 不再隐藏

ios - 将 UICollectionView header 中的 UISearchBar 隐藏在 NavigationBar 后面

ios - 显示具有 2 列大小相等的方形单元格的 UICollectionView

objective-c - 单击返回按钮时添加功能 -ios

ios - ios 中的可滑动 View

ios - 当父 View 改变大小时,如何告诉 subview 调整大小?

swift - UIButton 心跳动画

WPF同步动画和UI线程死锁

ios - UISearchBar 在关闭后正在调整大小