UISearchController - 搜索栏应始终可见

use*_*037 7 uitableview ios swift uisearchcontroller

目标:使搜索栏始终可见.

  1. 视图控制器嵌入在导航控制器中
  2. 不存在表视图
  3. 当用户点击搜索栏时,搜索栏需要从其当前位置动画到导航栏的顶部(向上移动),就像在iOS联系人应用中一样.
  4. 使用 UISearchController

我做了什么:

  1. 我已将搜索栏添加到视图控制器的视图中
  2. 我手动呈现搜索视图控制器(下面的代码)

问题:

  • 我无法控制动画,目前搜索栏从屏幕顶部向下移动到导航栏的位置.

预期行为:

  • 能够通过将搜索栏从当前位置向上移动到导航栏来为搜索栏设置动画

  1. 我的方法是否正确?
  2. 如何设置动画并从当前位置向上移动搜索栏?

码:

    func setupSearchController() {
        searchController.searchResultsUpdater = self
        self.definesPresentationContext = false
        searchController.delegate = self
        searchController.hidesNavigationBarDuringPresentation = true

        let searchBar = searchController.searchBar

        view.addSubview(searchBar)

        searchBar.translatesAutoresizingMaskIntoConstraints = false

        searchBar.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
        searchBar.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
        searchBar.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true

    }

    func presentSearchController(searchController: UISearchController) {
        let searchBar = searchController.searchBar

        searchBar.removeFromSuperview()

        let baseView = searchController.view

        baseView.addSubview(searchBar)

        searchBar.leadingAnchor.constraintEqualToAnchor(baseView.leadingAnchor).active = true
        searchBar.trailingAnchor.constraintEqualToAnchor(baseView.trailingAnchor).active = true
        searchBar.topAnchor.constraintEqualToAnchor(searchController.topLayoutGuide.bottomAnchor).active = true

        self.presentViewController(searchController, animated: true) {

        }
    }
Run Code Online (Sandbox Code Playgroud)

E-R*_*die 7

我不太关注你的逻辑,但我可以说的是你可以很容易地实现这一点.

  • 在您的Interface Builder上创建一个UIView将是searchControllers 的容器searchBar并设置约束如下:

在此输入图像描述

  • 将其连接UIView到,UIViewController并将其命名为searchBarContainer
  • 创建UISearchController要使用的属性:var searchController: UISearchController! = nil
  • searchController.searchBar添加到searchBarContainer中

幸运的是你的控制器应该是这样的:( 请注意,这里我没有在这里实现代理,但你可以照顾它,因为这与动画无关:))

class SearchViewController: UIViewController {
    var searchController: UISearchController! = nil
    @IBOutlet weak var searchBarContainer: UIView!

    // MARK: - View Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController = UISearchController(searchResultsController: nil)
        self.searchBarContainer.addSubview(searchController.searchBar)
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        // Setup the frame each time that the view appears in order to fit the width of the screen
        // If you use autolayout just call searchController.searchBar.layoutIfNeeded()
        var frame = searchController.searchBar.bounds
        frame.size.width = self.view.bounds.size.width
        searchController.searchBar.frame = frame
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

我的解决方案

在此输入图像描述

iOS通讯录

在此输入图像描述